Page 1 of 1

Batch file deletion

Posted: 27 Apr 2023 22:43
by stalker989
Hi all,

I am facing an issue with my batch script doing a file deletion on Windows XP. The script has 2 roles: 1) Archive all the files in the given directory and moved to a different location. 2) delete the original files with 2 exceptions with a specific filename: evs and inspection.
This is the code:

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion

SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
SET SrcDir=C:\Documents and Settings\Administrator\Desktop\BatchTest\Test
SET DestDir=C:\Documents and Settings\Administrator\Desktop\BatchTest\Archive-%TODAY%

IF NOT EXIST "%DestDir%" MD "%DestDir%"
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO "C:\Program Files\7-Zip\7z.exe" a -tzip "%DestDir%\%%~NXA.zip"  "%%~A"
FOR %%A IN ("%SrcDir%\*.*") DO @set file=%%A && if !file!==!file:pack=! if !file!==!file:inspection=! (DEL /A !file!)

ECHO.
PAUSE
This code works as intended on Windows 7/Windows 10, but I cannot make it work on XP. The script throws the "System cannot find the path specified" for each file that meets the criteria (except the 2 mentioned above) and will not delete them.
All I know is with an ECHO it will show the files that need to be deleted the full path.
My guess is it has to do something with Windows XP since its old, but I cannot find a solution for this.

Any help would be appreciated.

Re: Batch file deletion

Posted: 28 Apr 2023 15:46
by penpen
Ad hoc i can't see a statement or command that XP would not support, or use differently than the newer versions.
The only potential risks of incompatability i see are:
- The usage of the localization dependend variables "date" and "time" (so it could happen on all windows versions).
- External executables that you might use in different versions, so the command line parameters might have changed.

Ad date and time variables, for example under my localization, you cut away the following (unusual) parts (encapsulated in square brackets "[removed]"):

Code: Select all

variable = content     --> cut away mask for TODAY --> result
date     = 28.04.2023  --> [28.0]4.[2]02[3][]      -->  4.-02-
time     = 23:37:54,16 --> [23:]37[:]54[,]16[]     --> 375416

TODAY=4.-02--375416
Beside that the (environment) variable hr is undefined in your example, which might also cause an error in case you have more code and hr is defined at that point.

Re: Batch file deletion

Posted: 01 May 2023 14:36
by stalker989
Thanks for the hint @penpen,

After searching high and low and learning new things, I managed to fix my issue.

Updated script:

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion

Set "CurrentFolder=C:\Documents and Settings\Administrator\Desktop\BatchTest\Test"
CD /d %CurrentFolder%

SET TODAY=%DATE:~7,2%-%DATE:~4,2%-%DATE:~10,4%-%time:~0,2%-%time:~3,2%-%time:~6,2%

SET SrcDir=C:\Documents and Settings\Administrator\Desktop\BatchTest\Test
SET DestDir=C:\Documents and Settings\Administrator\Desktop\BatchTest\ArchiveLogs-%TODAY%

IF NOT EXIST "%DestDir%" MD "%DestDir%"

FOR %%A IN ("%SrcDir%\*.*") DO "C:\Program Files\7-Zip\7z.exe" a -tzip "%DestDir%\%%~NXA.zip"  "%%~A"

@for %%d in ("%CurrentFolder%\*") do (

set "file=%%d"

if "!file!"=="!file:evs=!" if "!file!"=="!file:insp=!" (
del "%%d"
)
)

ECHO.
PAUSE 
Long story short, for XP the " (quote) is very important. Even if echo returns a valid path, if the full path is not encased in "" it will not recognize it and will throw the cannot find the specified path.

Below the shorter version for excluding certain filenames and deleting the rest:

Code: Select all

@ECHO off
setlocal EnableDelayedExpansion

SET "fold=C:\Documents and Settings\Administrator\Desktop\BatchTest\Test"

@FOR %%A IN ("%fold%\*.*") DO (

set "file=%%A"

if "!file!"=="!file:evs=!" if "!file!"=="!file:insp=!" (
del "%%A"
)
)

echo.
pause
Thanks again.