I've got a problem with setting the path variable for the destination I want to copy files within a forfiles command to (cmd /c copy #path destination).
The code:
REM source folder location
set source=\\Path\TO\File\SOURCE\FOLDER
REM destination folder location
set set destination=E:\LOCAL\FOLDER\
REM log_file location
set log_file=E:\LOCAL\FOLDER\Logfile.txt
SET /a "copy_count+=0"
PUSHD %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy #path 0x22%destination%0x22 & echo #file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD
fails to copy the files in the designated directory but passing the Path directly into the command like:
.....
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy #path E:\LOCAL\FOLDER\ & echo #file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD
works.
How would I need to pass the variable correctly?
Edited to working code after comments from #Compo
REM source folder location
set "source=\\Path\TO\File\SOURCE\FOLDER"
REM destination folder location
set "destination=E:\LOCAL\FOLDER\"
REM log_file location
set "log_file=E:\LOCAL\FOLDER\Logfile.txt"
SET /a "copy_count+=0"
PUSHD %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If #IsDir==FALSE copy #path %destination% & echo #file %date%, %time%>>%log_file%"
') do SET /a "copy_count"+=1
POPD
To clarify on #aschipfl comment because I did not realize the time stamp stays the same throughout the whole process - Is this what your comment implied?
SET /a "copy_count+=0"
PUSHD %source%
(for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If #IsDir==FALSE copy #path %destination% & echo #file %date%, %time%)>>%log_file%"
') do SET /a "copy_count"+=1
POPD
Here's a quick example showing the methodology, I implied and suggested throughout my comments.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "source=\\Path\TO\File\SOURCE\FOLDER"
Set "destination=E:\LOCAL\FOLDER"
Set "log_file=E:\LOCAL\FOLDER\Logfile.txt"
PushD "%source%"
Set "copy_count=0"
(For /F "Tokens=1,*" %%G In ('%SystemRoot%\System32\forfiles.exe /S /D -2 /C ^
"%SystemRoot%\System32\cmd.exe /D /C If #IsDir==FALSE Echo #Path 0x25DATE0x25, 0x25TIME0x25"'
) Do Copy /Y %%G "%destination%" 1> NUL 2>&1 && Set /A copy_count+=1 & Echo %%~nxG %%H) 1> "%log_file%"
PopD
Related
I know someone out there has a nice quick solution for this: I just believe I am missing something. I am slightly new to creating bat files. I have roughly 2,000 files with improper naming on a share drive.
The below bat gets me started but I need to have the new file name include spaces but when I try it gives an invalid syntax command even with the ' " '.
Line 3, 3.MRF, is where it is giving me the syntax error '-' is not a valid command.
#echo
forfiles /S /M "1.PURCHASE*.*" /C "cmd /c rename "1.PURCHASE*.*" "2.PURCHASE*.*""
forfiles /S /M "3.MRF*.*" /C "cmd /c rename "3.MRF*.*" "1.MRF - *.*""
forfiles /S /M "3.ACOMRF-*.*" /C "cmd /c rename "3.ACOMRF-*.*" "1.ACO-MRF-*.*""
forfiles /S /M "2.RECEIPT*.*" /C "cmd /c rename "2.RECEIPT*.*" "3.RECEIPT*.*""
forfiles /S /M "2.RECIEPT*.*" /C "cmd /c rename "2.RECIEPT*.*" "3.RECEIPT*.*""
:exit
#pause
Perhaps run a for loop instead, it just looks much better to start off with:
#echo off
for /r /d %%a in (*) do (
pushd "%%~a"
ren "1.PURCHASE*.*" "2.PURCHASE*.*"
ren "3.MRF*.*" "1.MRF - *.*"
ren "3.ACOMRF-*.*" "1.ACO-MRF-*.*"
ren "2.RECEIPT*.*" "3.RECEIPT*.*"
ren "2.RECIEPT*.*" "3.RECEIPT*.*"
popd
)
Here you need one loop that will pushd to each of the subdirectories, rename anything that matches the rename parameter.
I would suggest using String Manipulation within a simple for-loop.
#echo off
setlocal enabledelayedexpansion
FOR %%A IN (3.MRF*.*) DO (
SET name=%%A
rename "!name!" "!name:3.MRF=1.MRF - !"
)
Before:
3.MRF A.txt
3.MRF B.txt
3.MRF C.txt
After:
1.MRF - A.txt
1.MRF - B.txt
1.MRF - C.txt
Update
Made a more generic solution which lets you define all search and replace strings. Moreover it works on all subdirs now.
#echo off
setlocal EnableDelayedExpansion
PUSHD "%0\.."
REM <search_1>;<replace_1|<search_2>;<replace_2>|...|<search_n>;<replace_n>
SET SearchAndReplace=3.MRF - ~1.MRF - ;3.ACOMRF-~1.ACO-MRF-
REM Replace ; with \n
SET SearchAndReplace=!SearchAndReplace:;=^
!
REM iterate over search and replace tokens
FOR /F "tokens=1,2 delims=~" %%G IN ("!SearchAndReplace!") DO (
set search=%%G
set replace=%%H
FOR /F "usebackq delims=" %%I IN (`dir /S /B /A:-d "*!search!*" 2^>nul`) do (
set file="%%I"
REM hack to allow replace !replace! for !search! in !file!
FOR /F "delims=" %%J IN ("!search!") do (
FOR /F "delims=" %%K IN ("!replace!") do (
echo move !file! !file:%%J=%%K!
move !file! !file:%%J=%%K!
)
)
)
)
POPD
Set the SearchAndReplace variable as needed (see comment).
I have a folder MyFolder and it contains 3 sub-folders say A,B and C.
I want to copy those sub-folders which are modified today.
The command for /D %%A in ("D:\MyFolder*") do ( echo **%%~fA )** provides me all 3 sub-folders, now I am calling another loop and passing %%~fA(which is current folder in loop) to get to know is there any modifications made in this folder or not. But I am getting echo off.
set LocalFolder=D:\Backup\1
for /D %%A in ("D:\MyFolder\*") do (
echo %%~fA
for /F %%N in ('forfiles /S /P "%%~fA" /M "*" /D +0 /C "cmd /C if #isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%
if %%N GTR 0 xcopy "%%~fA\*.*" "%LocalFolder%\" /s/e/k/f/c
)
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
I want to write a batch that finds all docs less than 50 mb in c:\ and copy them in a folder but ignore system directory docs. I prefer it does not even search in the system dir.
Here is my batch that finds and copies all files less 50 mb in right directory but i can not make it to ignore system from searching or C:\Windows directory.
#ECHO off
:: variables
SET odrive=%odrive:~0,2%
SET backupcmd=xcopy /s /c /d /e /h /i /r /y
MKDIR "C:\Users\Documents\USBBackups\DOC\C"
forfiles /P C:\ /M *.DOC* /S /C "cmd /c if #fsize leq 50000000 echo #PATH " > "C:\Users\Documents\USBBackups\DOCC.txt"
FOR /F "tokens=*" %%a in (C:\Users\Documents\USBBackups\DOCC.txt) do xcopy %%a "C:\Users\Documents\USBBackups\DOC\C" /c /h /i /r /y
#ECHO off
There is no way to tell forfiles to exclude certain directories when switch /S is provided. You will have to write your own code that does that.
I would not use forfiles for that due to poor performance, but standard for instead:
#echo off
for /D %%D in ("%SystemDrive%\*.*") do (
if /I not "%%D"=="%SystemRoot%" (
pushd "%%D"
for /R %%F in ("*.doc?") do (
if %%~zF LEQ 50000000 (
echo %%F
)
)
popd
)
)
Here the root directory level is enumerated by for /D. All directories other than %SystemRoot% are enumerated recursively by for /R.
I changed the search pattern from *.doc* to *.doc? in order not to include files ending in .doc.lnk for example, which I guess you do not want to be retrieved.
Instead of the echo command you can directly place your xcopy command line with "%%F" provided as the copy source.
You can do the same directly in command prompt as a one-liner, like this:
for /D %D in ("%SystemDrive%\*.*") do #if /I not "%D"=="%SystemRoot%" pushd "%D" & (for /R %F in ("*.doc?") do #if %~zF LEQ 50000000 echo %F) & popd
I recommend not to walk through the entire directory tree and later filtering by something like findstr /V /I /L /B /C:"%SystemRoot%", because in that case you were wasting time enumerating a huge number of items which you ignore afterwards.
However, if you do want to rely on forfiles /S, the working command line looks like this:
2> nul forfiles /S /P "C:\\" /M "*.doc*" /C "cmd /C if #isdir==FALSE if #fsize LEQ 50000000 echo #path" | findstr /V /I /L /B /C:"\"%SystemRoot%"
Adapt this technique of using findstr to filter out certain names.
To see size of folders in Documents, excluding music, video, or pictures folders.
for /f "skip=3 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"') do set doc=%A
for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"^|findstr /l /v "Pictures Music Video"`) DO #echo %j&echo.
However you could start the forfiles command in the c:\users or the particular users home folder (%userprofile%). You specify to start at c:\ which includes all folders.
forfiles /P %userprofile% /M .DOC /S /C "cmd /c if #fsize leq 50000000 echo #PATH "
forfiles /P c:\users /M .DOC /S /C "cmd /c if #fsize leq 50000000 echo #PATH "
i am trying to make a script to remove empty folders and delete files a number of days old. depending on what the txt file delimiters are set to. I have came up with this so far:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /m *.* /d %2 /c "cmd /c %COMND1% #file"
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir %%1 /s /b /ad ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30
when i run the script #file will not populate and %%i will not populate, i am not sure what i am doing wrong. Help?
You made a couple of very small errors. In DEL_FOLDERS you used %%1 which meant that the argument was not expanded (you only needed one % here). You also did not handle the case where there are no files that match or the directories are empty. In the FORFILES command you put /m *.*; although the documentation says this is the default, the documentation is incorrect. Missing out the /m matches all files (the default) but by saying /m *.* you only match files with a dot!
My corrected version is:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /d %2 /c "cmd /c %COMND1% #file" 2> nul
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir "%~1" /s /b /ad 2^>nul ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30