I'm looking to create a bat file to compare local folders from a dir command with folders on a server, and then delete the local folders if they do not exist on the server. Additionally, it is having issues with directories including spaces, but I have not looked into this yet.
My current code is not working correctly. Could anyone provide some guidance please?
Code:
REM Search local directories for files, delete if not present on server
set n=0
set count=0
for /f %%a in ('dir /a:d /b %_Entry_Local_Status60_path%') do (
set folder[!n!]=%%a
set /A a+=1
set /A n+=1
set /A count+=1
)
set n=0
for /L %%a in (0,1,%count%) do (
echo !folder[%n%]!
if not exist %_Entry_Network_Status60_path%\!folder[%n%]! rmdir %_Entry_Local_Status60_path%\!folder[%n%]!
set /A n+=1
)
I use the following to compare files within different folders and delete the ones from first folder which does not exists on second one. With some adaptation probably you could use something similar with folders and not files. This is based on FC and you have to provide the two absolute paths:
echo off
set "Folder1=path\to\Folder1"
set "Folder2=path\to\Folder2"
for /f "delims=" %%F in ('dir /b "%folder2%"') do (
if not exist "%folder1%\%%F" (
fc /b "%folder1%\%%F" "%folder2%\%%F"
if "%errorlevel%" EQU "1" (
del "%folder1%\%%F" && echo Deleted "%%F"
)
) else (
del "%folder1%\%%F" && echo Deleted "%%F"
)
)
pause
Exit
You should take a look at the robocopy command. I think robocopy _source_ _target_ /purge does exactly what you want.
Related
Want to make a batch file from a folder of 30,000 files from let's say C:\Users\NAME\Desktop, that will create a folder named "1", put 500 files in, no matter the name of the file, and then loop naming the next "2", until all 30,000 are in folders of 500 files each.
Thanks in advance for the help :)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
PUSHD "%sourcedir%"
ECHO sd=!sourcedir!
FOR /f "tokens=1*delims=:" %%b IN (
'dir /b /a-d * png *.doc^|findstr /n "."'
) DO (
SET /a subdir=1+(%%b / 7^)
FOR /f "tokens=2delims==" %%e IN ('set subdir') DO (
ECHO MD "%destdir%\%%e" 2>NUL
ECHO MOVE "%%c" "%destdir%\%%e\"
)
)
popd
GOTO :EOF
Always verify against a test directory before applying to real data.
I used a limit of 7 for testing. Modify as you will
Required md and move commands are merely echoed for verification. remove the echoes to activate after testing.
Found this from Compo, sorry didn't reference it!
#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=2"
For %%A In (*.png *.doc) 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
I am trying to find a solution to move all files, all types of extensions,from source folder to destination folder.
The problem is the scripts I found online are not checking if there are already files with the same name in destination folder and if it's the case to add the underscore and a variable number(1,2,3,4,5 and so on) at the end of the file.
I have something here that I tried to put together but it's not working. Batch script or Powershell doesn't matter. The requested outcome is to move all files and if file a.pdf exists in destination folder, move it to destination folder and add a variable number a_1.pdf or a_2.pdf
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Daniel\Desktop\testing"
SET "destdir=C:\Users\Daniel\Desktop\testing2"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
IF EXIST "%destdir%\%%~nxa" (
SET notfound=Y
FOR /L %%b IN (1,1,999) DO IF DEFINED notfound IF NOT EXIST "%destdir%\%%~na(%%b)%%~xa" (
(move "%%a" "%destdir%\%%~na(%%b)%%~xa"
SET "notfound="
)
IF DEFINED notfound ECHO(Failed to COPY "%%a"
) ELSE ((move "%%a" "%destdir%\%%~nxa"
)
)
pause
actual outcome it this
move "C:\Users\Daniel\Desktop\testing\1.jpeg" "C:\Users\Daniel\Desktop\testing2\1(1).jpeg"
move "C:\Users\Daniel\Desktop\testing\1.txt" "C:\Users\Daniel\Desktop\testing2\1(1).txt"
but when I look in the folder, nothing happens
Many thanks
#ECHO Off
SETLOCAL
SET "sourcedir=C:\Users\Daniel\Desktop\testing"
SET "destdir=C:\Users\Daniel\Desktop\testing2"
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
IF EXIST "%destdir%\%%~nxa" (
rem destfile exists
echo destfile "%destdir%\%%~nxa" exists
SET "notfound=Y"
FOR /L %%b IN (1,1,999) DO IF DEFINED notfound IF NOT EXIST "%destdir%\%%~na(%%b)%%~xa" (
move "%%a" "%destdir%\%%~na(%%b)%%~xa"
SET "notfound="
)
IF DEFINED notfound ECHO Failed to COPY "%%a"
) ELSE (
rem destfile does not exist
echo destfile "%destdir%\%%~nxa" does NOT EXIST
move "%%a" "%destdir%\%%~nxa" >nul
)
)
GOTO :eof
This worked for me. Frankly, I'm too tired to work out the differences.
Notes :
I overrode the directoryname values.
I added rems and step-by-step reporting, which obviously could be removed
Caution : code executes move statements as posted.
I have the multiple images in sub folder, I don't know how many folders are there. I want to Batch script to find listed names in all the folders and copy the images to destination folder. I tried below script but am getting file not found error.
#echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
Can any one fix this problem? Thanks in advance.
The solution you're trying to get to work seems excessively convoluted
I believe something similar to the following should do what you want
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
To ensure the filename matches are exact, all filenames in filenames.txt should be preceded by backslash, e.g.
\filename1.file
\filename2.file
You can easily generate such a file within a batch file, if necessary
I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"
I'm working on a script for copying files in a folder which corresponds to the file's name and I have that part worked out using a FOR loop which checks to make sure the destination folder exists and copies the files once it has verified that it does. Example: 11-01111_ABC_DEF.pdf would go into /11/0111/. The length may vary by one or two characters but the format is consistent. I've copied my basic script below.
My problem is that sometimes a file with the same name needs to be processed. How could I go about concatenating a number to the end of the file if one or more copies of the file already exists in the destination folder?
setlocal enableextensions enabledelayedexpansion
for %%x in (*.PDF *.TXT) do (
set "source="C:\files"
set "dest=R:\"
set "filename=%%x"
set "prefix=!filename:~0,2!"
set "folder=!filename:~3,5!"
if not exist !dest!\!prefix!\!folder! MOVE !filename! !source!\failed
if exist !source!\!filename! MOVE !filename! !dest!\!prefix!\!folder!
)
setlocal ENABLEDELAYEDEXPANSION
set "dest=worked"
for %%i in (*.pdf *.txt) do (
for /f "tokens=1-3* delims=-_." %%j in ("%%i") do (
if exist "%dest%\%%j\%%k" if exist "%dest%\%%j\%%k\%%i" if exist "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" (
for /f "tokens=1-9* delims=-_." %%n in ('dir %dest%\%%j\%%k\%%j-%%k*-* /b') do set /a inc=%%p
set /a inc+=1
move "%%i" "%dest%\%%j\%%k\%%j-%%k-!inc!_%%l_%%m" || move "%%~i" "failed"
set inc=
)
if exist "%dest%\%%j\%%k\%%i" if not exist "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" (
move "%%i" "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" || move "%%~i" "failed"
)
if exist "%dest%\%%j\%%k" if not exist "%dest%\%%j\%%k\%%~i" (
move "%%~i" "%dest%\%%j\%%k\%%~i" || move "%%~i" "failed"
)
if not exist "%dest%\%%j\%%k" move "%%~i" "failed"
)
)
put this in the folder with the .txts and .pdfs comment if you want me to change something.