sio2deep
sio2deep

Reputation: 113

Windows cmd FORFILES not working in batch file,but works in command prompt

In the below batch script the line with command FORFILES is not working. The same statement works fine when it is run separately from a different batch file or from command prompt. All other statements in the script works fine. I have gone through all solutions for similar problems.

@echo off
setlocal

SET vFileShare=C:\Users\asande\task\
SET archiveFileList=ArchiveFilesList.txt
SET archFileTimestamp=%date:~10,4%%date:~7,2%%date:~4,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
rem SET archFileName=%fSignature%%archFileTimestamp%
SET archiveFolder=C:\Users\asande\task\archives\
SET PATH=C:\Program Files\7-Zip

FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del @file" /d -1

IF EXIST %vFileShare%%archiveFileList%. (
            cd %vFileShare%
            7z a -tzip %archiveFolder%%archFileTimestamp%.zip @ArchiveFilesList.txt
) ELSE (
     ECHO %archiveFileList% missing.>>%vFileShare%\Polaris_DatedConversionRate.log.
 )


endlocal

GOTO: EOF

Upvotes: 2

Views: 14033

Answers (3)

docker
docker

Reputation: 1

SET archiveFolder=C:\Program Files\7-Zip
FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del @file" /d -1

Upvotes: -1

sio2deep
sio2deep

Reputation: 113

Got the answer, the problem actually is due to following 2 statements

SET PATH=C:\Program Files\7-Zip

FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del @file" /d -1

SET PATH ...... statement should be after FORFILES command . FORFILES command will not work even in command prompt if you set PATH to some value before running FORFILES. (it throws FORFILES is not recognized as an internal or external command.)

Can anyone enhance the answer why PATH does so. And i think it effects some other commands like this.

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200573

@file is just the file name. Your working directory is probably different from %archiveFolder%, so del doesn't find the files it's supposed to delete, because it's looking in the wrong place (%CD% instead of %archiveFolder%). Try using @path instead:

forfiles /p "%archiveFolder%" /m *.zip /d -1 /c "%comspec% /c del @path"

Upvotes: 0

Related Questions