Create a batch file which synchronize two folders using text file validations - batch-file

I want to create such scenarios in which there are two folders & subfolders of A and B, when there is an addition of files or data in folder A it should create the same files or data in folder B (including subfolder). Also, for example, there are 6 files in folder A and 7 files in folder B system automatically check the files and subfiles of folder A and B and delete the mismatched file(pqr.js
) from 'Folder B'. We also want to create a bridge or validator text file between folder A and B which validate the file names, for example if there is filename 'config.properties' which is present in text file (Validator) ABC.txt and that file is not present in folder A system should check the file name in validator before deleting any files from folder B. So if there are no such file name config.properties in folder A but present in validator text file system will keep that file in folder B though it is deleted from folder A.
set dirsrc=folder A
set dirdest=folder B
set validator=ABC.txt
setlocal ENABLEDELAYEDEXPANSION
echo Source: "%dirsrc%"
echo Destination: "%dirdest%"
echo.
echo.
echo Cleaning files...
for /f "tokens=*" %%a in ('dir /s /b /a:-d "%dirdest%"') do (
set curdest=%%a
set cursrc=!curdest:%dirdest%=%dirsrc%!
if not exist !cursrc! (
echo Deleting "!curdest!"
del "!curdest!"
)
)
echo.
echo.
echo Cleaning directories...
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem Two lines left empty for NewLine to work
for /f "tokens=*" %%a in ('dir /s /b /a:d "%dirdest%"') do (
set curdest=%%a
set cursrc=!curdest:%dirdest%=%dirsrc%!
if not exist !cursrc! (
set rmlist=!curdest!?!rmlist!
)
)
set rmlist=!rmlist:?=%NL%!
for /f "tokens=*" %%a in ("!rmlist!") do (
if exist %%a (
echo Removing directory "%%a"
rmdir "%%a"
)
)
xcopy /i /y /e "%dirsrc%" "%dirdest%" /d
endlocal
All the scenarios are completed expect Validator scenarios.

Related

If zip file and folder name are identical, copy zip file into folder

I have many .zip files and folders some of them have identical names.
I will check zip file name and folder name if it is identical.
I will copy zip file into the folder and delete the zip file.
Is it possible to Automate the process?
I have found following code but couldn't edit it:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.zip DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a Random Center"
for /D %%i in (*.*) do (
for /F "tokens=1 delims= " %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
It creates Random Center folder and copy all the zip files in it which I don't want.
#echo off
setlocal
pushd "C:\New folder" || exit /b 1
for %%A in (*.zip) do if exist "%%~nA" (
pushd "%%~nA" && (
move /y "..\%%~nxA"
popd
)
)
popd
pause
Using move as see no need to
copy and del the zip file.
Argument /y will automatically
overwrite an existing file without
prompt.
Initial pushd changes the current directory
else exits with errorlevel 1.
The for loop iterates through each zip filename.
It checks if the name of the filename, without extension,
does exist, which is expected to be a folder.
If does exist, pushd into the directory, and then
move the zip file from the parent directory into
the current directory. popd will restore to the
previous directory.
Does final popd to restore to initial current directory.

Count a number of pdf files in every subfolder separately in batch

Under the path U:\test\0014* I have 99 folders and each of them has respectively 2 subfolders MASTER and DERIVATIVE_COPY. With a following script I try to count the number of PDFs in MASTER folder. If there are only one .pdf file there I want to copy it to DERIVATIVE_COPY folder. IF there are 0 or >1 .pdf in MASTER I want only to show the number of them. This operation should be done for each of the 99 folders.
#echo off
setlocal enabledelayedexpansion
for /R U:\test\0014\*\MASTER %%i in (*.pdf) do (
set /a anzahl+=1
)
if !anzahl! EQU 1 ( echo !anzahl! )
if NOT !anzahl! EQU 1 ( echo !anzahl! )
pause
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir"
for /f %%i in ('dir /ad /s/b "%sourcedir%\master*"') do IF /i "%%~nxi"=="MASTER" (
SET /a found=0
FOR %%x IN ("%%i\*.pdf") DO SET /a found+=1
IF !found!==1 (
XCOPY /y "%%i\*.pdf" "%%i\..\derivative_copy\" >nul
) ELSE (
ECHO !found! .pdf files found IN "%%i"
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
First, execute a dir to get a directory-list of the "files" names that start master in the specified tree. Use /ad to select only the directory-names. Accept only those names where the "name+extension" is master (disregarding case)
For each directory-name found, set found to 0 then increment found for each .pdf file found in the directory %%i.
If the resultant count in found is 1, xcopy the file found to the destination subdirectory (which conveniently creates the subdirectory if it doesn't already exist), using /y to overwrite any existing file of the same name and >nul to make the process silent.
Otherwise, report the directoryname and count of files.

Batch File Rename and Directory Autocount

I need a way to make a directories containing the filename, but counting +1 for every one created.
How can i add +1 in front of the directory name created
Eg:
if Directory 1_xxx is present make 2_xxxxx
Currently i am using below to create a directory from Filename
File name Eg: Card_24ALL Sms.csv
Directory Output Eg: Card_24ALL Sms
Currently i have directories adding +1 manually 1_
1_Card_24ALL Sms ( With File Card_24ALL Sms.csv copied inside)
2_Card_25ALL Sms ( With File Card_25ALL Sms.csv copied inside)
3_Tjek_24ALL Sms ( With File Tjek_24ALL Sms.csv copied inside)
Using the below Code i can get directory added containing the given filename:
#echo off
for %%a in (*.csv) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
But I have to manually create the number in front to get number_directory as the number in front can be 99_
Try this:
#echo off
setlocal enabledelayedexpansion
#echo off
cd /d c:\temp
if exist cnt.txt for /f %%a in (cnt.txt) do set %%a
for %%a in (*.csv) do (
set /a cnt+=1
echo md "!cnt!_%%~na" 2>nul
echo move "%%a" "!cnt!_%%~na" >nul && (
Echo File %%~nxa moved to !cnt!_%%~na) || (Echo Move failed.)
)
>cnt.txt echo cnt=!cnt!
Remove the echo's from md and move once you verify the output.

batch file to delete files and folders except zip files

I've created a batch file which unzips all files
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
REM Searching directory structure from root for subfolders and zipfiles,
REM then extracting the zipfiles into a subfolder of the same name as the zipfile.
FOR /F "delims==" %%d IN ('dir /ogne /ad /b /s "%ROOT%"') DO (
ECHO Extracting : "%%d"
FOR /F "delims==" %%f IN ('dir /b "%%d\*.zip"') DO (
REM Getting filename without extension.
SET subfolder=%%~nf
ECHO mkdir "%%d\!subfolder!"
mkdir "%%d\!subfolder!"
REM Extracting zipfile content to the newly created folder.
ECHO 7z x "%%d\%%f" -o "%%d\!subfolder!"
"C:\Program Files\7-Zip\7z.exe" x "%%d\%%f" -o"%%d\!subfolder!"
)
)
ENDLOCAL
Now I want to delete the files which has been created.
Can anyone help in deleting the unzipped files alone and not the zipped files and also care should be taken that it should not delete the root and the zip file.
Any other solution is also appreciated.
I want the zip files only within the folders. All other files can be deleted without user saying Y/N.
Try this and remove the echos if the output is OK:
for /r "%root%" %%i in (*.zip) do (
echo del /f /q "%%~dpni\*.*"
echo rd /q "%%~dpni"
)

How to copy files from a random folder into a destination folder in batch?

I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!

Resources