Batch copying with xcopy - Renaming in use files - batch-file

What I want to be able to do is have the script rename a files that are in use.
Is this possible?
This is my current script.
Thanks in advance!
#echo off
Set FileSource=C:\temp\cleric
Set RemoteDestinationFolder=C:\program files (x86)\cleric
Set InputFile=c:\Test\Computers.txt
Set RemoteDestination=\\%%*\%RemoteDestinationFolder::=$%\
For /F "eol=;" %%* in ('type "%InputFile%"') do (
Ping.exe -n 2 -w 750 %%* | Find/i "TTL=" >Nul&&(
xcopy /Y/q/r/e "%FileSource%" "%RemoteDestination%")||(
echo.%%* "Computer was Not found">>%InputFile%.errors.log)
If Not ErrorLevel 0 echo.%%* "Copy was unsuccesfull">>%InputFile%.errors.log
(Set ErrorLevel=)
)
#echo.
Pause

Related

How do I count files in 2 different directories and do a compare using batch file?

I am trying to compare the number of files on a local folder to that of the source folder which is in a server.
I have the following lines in a batch file below but i always get the same value for both no matter what.
#for /f %%a in ('2^>nul dir "%local_folder%" /a/b/-o/-p/s^|find /v /c ""') do set n=%%a
echo Total files in Local Folder: %n%
pause
#for /f %%a in ('2^>nul dir "%source_folder%" /a/b/-o/-p/s^|find /v /c ""') do set m=%%a
echo Total files in Source Folder: %m%
pause
if %n%==%m% (copy update_folder local_folder.\ && goto end) else (goto copy_all_files)
pause
I am getting the same value for when I try to count the files in the separate locations. I only want to count the visible files with extension *.wav on each folder.
UPDATE: RESOLUTION I found is below,
:file_count_chck
set folder_cf=".\folder-A\*.wav"
set folder_lib="\\folder-B\*.wav"
dir %folder_cf% > A.txt
dir %folder_lib% > B.txt
set count_cf=0
for /f %%x in (A.txt) do set /a count_cf+=1
set count_lib=0
for /f %%y in (B.txt) do set /a count_lib+=1
set data_lines=5
set /A count_cf = %count_cf%-%data_lines%
set /A count_lib = %count_lib%-%data_lines%
echo.
echo Checking Sound folder...
echo Sound files on CF card = %count_cf%
echo Sound files on library = %count_lib%
del "A.txt" "B.txt"
set /a difference_count=%count_lib%-%count_cf%
if %difference_count% EQU 0 (echo Ok && goto wav_update) else (echo Detected missing files and correcting && goto copy_all_wav)
Hope someone finds this useful.

Break a folder with many files to multiple subfolders (Windows 10)

I have a folder with 110.000 files and I want a way to break this folder into multiple subfolders containing say 3000 files each (with a batch script perhaps?). (Trying a copy/paste with WinExplorer gets stuck in "Preparing to Copy".)
For example:
BigFolder
|
NewFolder
| | | | |
Sub1 Sub2 Sub3 Sub4 Sub5...
I am surprised to find the same case of mine.
I had 30,000 files that needed to be sorted, so I asked question on this page:
Fast methods to copy(move) files in batch file
This is Compo's script:
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=700"
For %%A In (*.bat *.cmd *.txt) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
And this is what I use and I edited for a bit for the purpose:
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
taskkill /f /im explorer.exe >nul
taskkill /f /im SearchIndexer.exe >nul
sc stop WSearch >nul
sc config WSearch start= disabled >nul
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
cls
echo Moving files to Directory %DirN%...
Set "limit=2999"
MD "%DirN%"
For %%A In (*.html) Do (
RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
start explorer.exe
start SearchIndexer.exe
sc config WSearch start= delayed-auto >nul
sc start WSearch >nul
Timeout -1 1>Nul
You can remove taskkill, start and sc part if desired.
I added this part because explorer and Windows Search Indexer will cause waste of memory when moving files. I recommend you to run the script with Administrator privilege.
Try to test the script in small scale to see if it does work.

Check for new folder after running bat commands

Is there a way to check if a new folder (exact name is unknown) has been created inside a specific parent folder as a result of running previous commands in bat file? If yes - run one more command with the full path to a new folder as an argument to that command.
Right now I am using a folder monitoring software to run another bat if new folder is created. I would like to have just one script performing both tasks.
Thank you for your help.
change the root_folder location on the second line
#echo off
set "root_folder=C:\something"
setlocal enableDelayedExpansion
set counter=1
for /d /r "%root_folder%" %%a in (*) do (
set "dirs[!counter!]=%%~sa" >nul
set /a counter=counter+1
)
rem ############################
rem # call your code here !!! #
rem ############################
call commands.bat
rem for /l %%l in (1,1,!counter!) do (
rem dir /x /b /s /a:d "%root_folder%" | findstr /i "dirs[%%l]"
rem )
set flag=0
for /d /r "%root_folder%" %%a in (*) do (
set dirs[|find /i "%%~sa" >nul 2>nul ||(
echo "%%~sa" is a new folder
set flag=1
)
)
if %flag% equ 0 (
echo no new folders
)

How to exclude a directory from a dir call loop--faster than |find /v

Right now I've got a batch script that is used to go through a bunch of subfolders, and zip up the logfiles therein, the folder structure is basically like this:
+---fakeG
| +---ExclusionFolder
| | +---LimitlessFolders
| | +---MoreFolders
| | \---SoManyFolders
| +---logs1
| +---logs2
| +---logs3
| +---logs4
| +---logs5
| \---logs6
Each of these subfolders must be traversed, and their subfolders traversed as well. This script has to avoid one specific folder, and it does avoid it, but it uses %| find /v. Although in this example, ExclusionFolder is at the top of the list, in the real folder structure it is not.
Here is how I do it now:
FOR /F "delims=" %%A IN (
'DIR "%LogsFilespec%" /B /S ^| find /v "ExclusionFolder"'
) DO CALL :DoZip "%%~dpnA" "%%~fA" "%zipCommand%" "%zipParams%" %ProcessLog%
Inside ExclusionFolder, there are more subfolders, with potentially many more subfolders, so it would slow down the scripts execution having to go into each of them.
SO: Is there a faster way to exclude a folder from a dir call without |find ?
Or do I have to come up with a totally different way to do this?
Edit Sorry, %LogsFileSpecâ„… refers to the target file. My original edit was right, and my second edit was wrong.
SET LogsLocation="G:\fakeG\Data\Logs"
SET LogsName="trace*%targetDate%.log"
SET LogsFilespec="%LogsLocation%\%LogsName%"
Sorry for not giving more of the script, I figured the question didn't need much.
Edit2 The process :DoZip works like this:
:DoZip
:: Parameter 1 = Filename without .EXT for Archive Name
:: Parameter 2 = Target file specifics
:: Parameter 3 = Zip Command (Winzip/7zip64/7zip32)
:: Parameter 4 = Zip Parameters (a -tzip, -a)
:: Parameter 5 = ProcessLog
setlocal
SET archiveName=%~1
SET SourceFileSpec=%~2
SET zipCommand=%~3
SET zipParms=%~4
SET RunLog=%~5
ECHO %TIME% Archiving %SourceFileSpec%...
ECHO %TIME% Archiving %SourceFileSpec%... >> %RunLog%
ECHO "%zipCommand%" %zipParms% "%archiveName%.zip" "%SourceFileSpec%"
ECHO "%zipCommand%" %zipParms% "%archiveName%.zip" "%SourceFileSpec%" >> %RunLog%
"%zipCommand%" %zipParms% "%archiveName%.zip" "%SourceFileSpec%" >> %RunLog%
:: Check errorlevel of executed command
:: If errorlevel != 0, set EC with the errorlevel and echo that there was an error in archival
IF NOT %ERRORLEVEL%==0 (
ECHO ***ERROR archiving %SourceFileSpec% >> %RunLog%
SET EC=%ERRORLEVEL%
ECHO ***ERRORLEVEL RETURNED: %EC% >> %RunLog%
) ELSE (
:: Otherwise, delete the file
ECHO. >> %RunLog%
ECHO. >> %RunLog%
ECHO %TIME% Deleting %SourceFileSpec%...
ECHO %TIME% Deleting %SourceFileSpec%... >> %RunLog%
::Quietly delete the file
DEL /Q %SourceFileSpec%
:: Set ErrorLevel to capture the Delete command result.
SET EC=%ERRORLEVEL%
)
GOTO :EOF
Edit 3 Here is zipCommand
SET PathWinZip=C:\Program Files\WinZip\wzzip.exe
SET Path7Zip_64bit=C:\Program Files\7-Zip\7z.exe
SET Path7Zip_32bit=C:\Program Files (x86)\7-Zip\7z.exe
:: Check for WinZip
IF EXIST "%PathWinZip%" SET zipCommand=%PathWinZip% & SET zipParms=-a
:: Check for 32-bit version of 7-Zip. If found, configure
:: its command line parameter to produce a .zip file
IF EXIST "%Path7Zip_32bit%" SET zipCommand=%Path7Zip_32bit% & SET zipParms=a -tzip
:: Check for 64-bit version of 7-Zip. If found, configure
:: its command line parameter to produce a .zip file
IF EXIST "%Path7Zip_64bit%" SET zipCommand=%Path7Zip_64bit% & SET zipParms=a -tzip
I believe that the following should exclude the subtree in question:
FOR /F "delims=" %%X IN (
'DIR /B /ad ') DO IF /i "%%X" neq "ExclusionFolder" FOR /F "delims=" %%A IN (
'DIR /B /S "%%X\%LogsFilespec%"'
) DO CALL :DoZip "%%~dpnA" "%%~fA" "%zipCommand%" "%zipParams%" %ProcessLog%
That is, perform a directory-name scan of the target; if the directory found is not the exclude-name, then do the remainder for that subdirectory.
This approach could be a bit faster:
pushd "%LogsFilespec%"
rem get subfolders of first level only
for /f "tokens=*" %%p in (
'DIR "%LogsFilespec%" /B /AD ^| find /v "ExclusionFolder"'
) do (
rem treat each (here will not be excluded one)
FOR /F "delims=" %%A IN (
'DIR "%%~p" /B /S'
) DO CALL :DoZip "%%~dpnA" "%%~fA" "%zipCommand%" "%zipParams%" %ProcessLog%
)
popd
goto :eof
The solution below don't use a single FIND command nor a FOR /F ... one (that requires the execution of a copy of cmd.exe), so I think it should run faster:
#echo off
setlocal
rem Enter to the base folder
cd "%LogsFilespec%"
rem Start the recursive process
call :processThisDir
goto :EOF
:processThisDir
rem Process the files in this folder
for %%A in (*.*) do CALL :DoZip "%%~dpnA" "%%~fA" "%zipCommand%" "%zipParams%" %ProcessLog%
rem Process nested subfolders, but omit "ExclusionFolder"
for /D %%a in (*) do (
if "%%a" neq "ExclusionFolder" (
cd "%%a"
call :processThisDir
cd ..
)
)
exit /B

batch-file incorporate another batch that copies modified files in directory to another directory

Here is what I've come up with, however I can't get the correct reference of xcopy in my code from what Aacini has provided in another post. cmd console will say it cannot find file "!lastName!!baseExt!" and then show that 0 files have been copied. It's not copying because I think the xcopy syntax will not allow for substitutions for the "source" "directory" relationship following xcopy.
Any help would be much appreciated. Thanks
#Echo Off
:: variables
set drive=C:\Users\me\Desktop\Test Source Folder
set backupcmd=xcopy /m /s /c /d /e /h /i /r /y /exclude:AutoFileCopy_Rev1.bat
set basename=
for %%a in ("C:\Users\me\Desktop\Test Source Folder") do (
if not defined baseName (
rem Is first name of first set
set baseName=%%~Na
set baseExt=%%~Xa
set lastname=%%~Na
) else (
rem Check if this name begin with same baseName
set name=%%~Na
for %%b in (!baseName!) do set name=!name:*%%b=!
if "!name!" neq "%%~Na" (
rem Yes: Is next name of same set
set lastName=%%~Na
) else (
rem No: Is first name of next set: copy previous set and pass to next one
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\! baseName!!baseExt!"
set baseName=%%~Na
set baseExt=%%~Xa
set lastName=%%~Na
)
)
)
rem Copy last set
Set _Delay=10
Set _Monitor=C:\Users\me\Desktop\Test Source Folder\
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
echo ___ Backing up JobBoss files...
::%backupcmd% "C:\Users\john.weakley\Desktop\Test Source Folder" "C:\Users\me\Desktop\Test Destination Folder\"
::CALL "C:\users\me\Desktop\Test Source Folder\Test.bat"
ECHO ___ Checking for new file revisions...
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\!baseName!! baseExt!"
Echo.Backup Complete!
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
You need to enable delayed expansion in order to support !VAR! syntax.
Change your first line to:
#echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

Resources