delete batch script not working - batch-file

#ECHO OFF
Set LOG="C:\Temp\Copy_Delete.log"
::##############################################
::Begin Deleting
::##############################################
Set Sourcedir="C:\test"
Echo %date% %time%: "Deleting from %sourcedir% >> %LOG%
FORFILES /P "%Sourcedir%" /D +0 /C "cmd /c del #path %Sourcedir%" >> %LOG%
When i executed this ,delete didn't work and in the log file it shows
C:\test*, Are you sure (Y/N)? and at the command prompt it gives this message "Fri 11/08/2013 16:11:43.28: "Deleting from "C:\test" >> "C:\Temp\Copy_Delete.log"
what could be the issue here.

Try passing quiet flag to del command.
del /Q #path %Sourcedir%" >> %LOG%

This worked under Windows 7 Pro:
#ECHO OFF
Set LOG="C:\Temp\Copy_Delete.log"
::##############################################
::Begin Deleting
::##############################################
Set Sourcedir="C:\test"
Echo %date% %time%: Deleting from %sourcedir% >> %LOG%
FORFILES /P "%Sourcedir%" /D +0 /C "cmd /c IF #ISDIR==FALSE DEL #path" >> %LOG%
It deletes all files modified today in C:\test .

Related

How to record purged files into a log file

I am using the following code in a batch file to purge any files older than 60 days. I would like the log file to print the file names that have been purged for record keeping purposes. How can I do that?
setlocal EnableDelayedExpansion
set logpath=C:\Temp\Archive
REM if not exist %logpath% md %logpath%
set FILEAGE=60
set archlog=%logpath%\Accurate_ARCHIVE.txt
set inputdir=C:\Temp
REM Set the date
FOR /f "tokens=2,3,4 delims=/ " %%a in ('date/t') do (set DD=%%a) & (set MM=%%b) & (set YYYY=%%c)
REM * Purge files older than %FILEAGE% days from disk.
forfiles /p "%inputdir%" /s /m *.* /d -%FILEAGE% /c "cmd /c del #path"
ECHO Folder %inputdir% was processed on %DD%-%MM%-%YYYY% >> %archlog%
ECHO Files older than %FILEAGE% days have been purged. >> %archlog%
exit %errorlevel%
I am expecting the log file to print the names of the files purged.
Add echo #path to the del #path command.
Something like below (untested).
"cmd /c (del #path) & (>> %archlog% echo #path)"
A more advanced version incorporating comments by Aschipf and Compo.
The 0x22 puts doupble quots around the filename and prevents problems with space and other special characters in the filename.
Deleted files are reported and files that could not be deleted are reported prefixed with NOT DELETED:.
"cmd /c if #isdir==FALSE (del #path) & (>> 0x22%archlog%0x22 if exist #path (echo NOT DELETED: #path) else (echo #path))"

Batch path variable does not work in Windows Server 2016

we recently switched from Windows Server 2008 to 2016 and we have some tasks to delete files and folders based on batch every day.
Here is the code:
set successful="D:\TEST\logs\successful.log"
set failed="D:\TEST\logs\failed.log"
set delpath="D:\TEST\1 und 2"
echo Logged time = %time% %date% >> %successful%
echo Logged time = %time% %date% >> %failed%
echo Files deleted: >> %successful%
echo Files failed to delete: >> %failed%
forfiles -p %delpath% -m *.* -c "cmd /c del /q #path && echo #path>>%successful% || echo #path>>%failed%"
forfiles -p %delpath% -c "cmd /c IF #isdir == TRUE rd /S /Q #path && echo #path>>%successful% || echo #path>>%failed%"
echo. >> %successful%
echo. >> %failed%
Since server 2016 this batch does not work.
Some issue with spaces in the path.
Output is:
ERROR: Invalid argument/option - 'und'.
Type "FORFILES /?" for usage.
Could there be some changes in syntax of the path? Did I miss quotation marks?
Here's a quick example of what I understand to be the same task, (emptying "D:\TEST\1 und 2" and logging both failed and successful actions); but using For loops instead of ForFiles commands. Please feel free to give it a try and report back as necessary.
#Echo Off
Set "successful=D:\TEST\logs\successful.log"
Set "failed=D:\TEST\logs\failed.log"
Set "delpath=D:\TEST\1 und 2"
(Echo Logged time = %TIME% %DATE%&Echo Files deleted:)>>"%successful%"
(Echo Logged time = %TIME% %DATE%&Echo Files failed to delete:)>>"%failed%"
For /D %%A In ("%delpath%\*")Do RD /S/Q "%%A" 2>Nul&&(>>"%successful%" Echo %%A)||>>"%failed%" Echo %%A
For %%A In ("%delpath%\*")Do Del "%%A" 2>Nul&&(>>"%successful%" Echo %%A)||>>"%failed%" Echo %%A
Echo(>>"%successful%"
Echo(>>"%failed%"
The issue has nothing to do with the name of the directory itself, but more with the fact if the directory is empty or not, as you can see in following experiment (I started adding an empty file "blabla.txt" in the directory, and I deleted it afterwards):
Prompt>forfiles /P "C:\1 und 2" /C "cmd /c echo #path"
"C:\1 und 2\blabla.txt"
Prompt>del "1 und 2\blabla.txt"
Prompt>forfiles /P "C:\1 und 2" /C "cmd /c echo #path"
ERROR: The system cannot find the file specified.

Batch file, removing double quote but giving blank .txt

My goal is to remove the double quote and send resource of .txt file as body mail through blat, I've seen a lot of question regarding this(removing double quotes).. but I can't figure out, where am I doing wrong. Here is my code
set "now=%date:~4%"
for /f %%i in ('FORFILES /D %now% /m *.csv /c "cmd /c echo #fname"')
do #set MyVariable=%%~i > C:\temp\count.txt
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Incoming_File_Alert" -to mymail#mail.com
EDIT:
This giving output blank.
EDIT 2 :
if I switch out line number 2 with this FORFILES /D %now% /m *.csv /c "cmd /c echo #fname" > C:\temp\count.txt
The output is like this
"407232_341600"
"TW39369763_341610"
"1726_341592"
"407316_341601"
"16001_341597"
"100001317_341590"
"407367_341602"
"DHB11838_341593"
"407439_341606"
"407556_341604"
"2373_341595"
"ALL1020-461_341614"
"407382_341605"
"3598_341613"
"PO051334_341589"
"407537_341607"
"407222_341598"
"TW39369964_341611"
"407403_341608"
You can give a try for this batch file :
#echo off
set "SourcePath=C:\Users\user1\Documents\Work\warehouse\"
set "now="
set "Ext=csv"
Call :GetCurrentDate
set "outputfile=C:\temp\count.txt"
If exist "%outputfile%" Del "%outputfile%"
CD /D "%SourcePath%"
#for /f "delims=" %%i in ('FORFILES /D %now% /m *.%Ext%') do (
echo %%~ni >> "%outputfile%"
)
If exist "%outputfile%" start "" "%outputfile%" & exit
::********************************************************************************
:GetCurrentDate
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set now=%DD%/%MM%/%YYYY%
exit /b
::********************************************************************************
Thanks to Squashman,
my problem solved with his suggestion.. looked like this, if anyone interested
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv')do >> C:\temp\count.txt echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com
EDIT 1:
Mistype.
EDIT 2:
Above is duplicating if we don't delete previous existing .txt file
here is adding syntax delete previous file, thanks to Hackoo answer
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
set "outputfile= C:\temp\count.txt"
If exist %outputfile% del %outputfile%
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv') do >> %outputfile% echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com

Batch file (ForFiles multiple conditional commands for logic)?

I am just getting into Bat files.
I am trying to delete old folders on a network shared drive but skip 2 of the containing folders by name.
Basically I need to all files that I make daily and always keep 2 old files.
Code that deletes all files that are older than 3 days:
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
And I was thinking something like this: if NOT #FNAME == %name%. I don't totally understand the process, am I able to have two conditions in the forFiles? do I have to have /c before?
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if NOT #FNAME == %name% if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
I can't seem to get it, would you mind helping me out?
Thanks!
Yes, nesting if commands in their then branches is the way how-to have logical AND. Note proper quoting in next code snippet:
#ECHO ON >NUL
#SETLOCAL enableextensions
set "name=SO"
set "nam2=SU"
pushd "D:\VB_scripts"
#rem all directories
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
#rem all directories except "SO"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" echo #FILE"
#rem all directories except "SO" and "SU"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" if not #FNAME=="""%nam2%""" echo #FILE"
popd
#ENDLOCAL
Output:
==>D:\bat\SO\31346676.bat
==>set "name=SO"
==>set "nam2=SU"
==>pushd "D:\VB_scripts"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
"Class Pack"
"Oldies"
"SO"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" echo #FILE"
"Class Pack"
"Oldies"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" if not #FNAME=="
""SU""" echo #FILE"
"Class Pack"
"Oldies"
"WMI"
==>popd
It is a little bit messy but I was able to do it on a local directory:
#echo off
mkdir Temp\Temp
REM Copy all old file to Temp dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c xcopy #path %cd%\Temp"
REM copy a random file from Temp dir to Temp/Temp dir and then delete it
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
DEL "%%A"
GOTO :Second
)
:Second
Rem copy second random file from Temp dir
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
GOTO :Del
)
:Del
Rem delete all old files from local dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c del #path"
Rem Copy back two random old files to local dir
xcopy %cd%\Temp\Temp\* %cd%
Rem remove Temp dir
rmdir /s /q Temp

for files Invalid argument/option - '>>'

#ECHO OFF
Set LOG="C:\Temp\Copy.log"
::########################
:: Begin copying
::########################
Set Sourcedir="c:\data\PRODDB\dir"
Set Targetdir="C:\test"
Echo %date% %time%: "Copying from %sourcedir% to %Targetdir%" >> %LOG%
Forfiles /P "%Sourcedir%" /D +0 /C "cmd /c copy #path "%Targetdir%" >> %LOG%
when i execute the above script ,i am getting the below error ERROR: Invalid argument/option - '>>'.
Type "FORFILES /?" for usage. what could be the issue here ? thanks
the problem is on the /C parameter. You don't close your double quotes correctly.
try
Forfiles /P "%Sourcedir%" /D +0 /C "cmd /c copy #path %Targetdir%" >> %LOG%
Both PA's answer and aphoria's answer will work in most cases. But they will fail if the Targetdir contains token delimiters like <space>, =, ;, etc. or poison characters like &. Quotes are needed in such cases. Aphoria's answer looks like it might work, but the extra quotes cause Targetdir to not be quoted in the first parsing pass.
The solution is to use the FORFILES 0xNN notation to represent the internal quotes.
Forfiles /P "%Sourcedir%" /D +0 /C "cmd /c copy #path 0x22%Targetdir%0x22" >> %LOG%
You're missing a quote at the end of your command
Try this:
Forfiles /P "%Sourcedir%" /D +0 /C "cmd /c copy #path "%Targetdir%"" >> %LOG%
Note the "" just before the >>.

Resources