move files from one folder to another and if exists rename - batch-file

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.

Related

Batch script to move the files from source to destination based on the file name [duplicate]

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 21 days ago.
The detail requirement is I need to move files from source folder to Destination folder based on the File names. For example
ABC_TEST1.txt should be moved to Y:\Dest\ABC, EFG_TEST1.txt should be moved to Y:\Dest\EFG, XYZ.txt should be moved to Y:\Dest\Unkonow, GHI_TEST1.txt should be moved to Y:\Dest\Unkown.
I tried the script below.
setlocal EnableDelayedExpansion
for %%F in (*.txt) do (
set FileName=%%F
echo %%F
if x%FileName:_=%==x%FileName%(
for /F "tokens=1* delims=_" %%B in ("%%~nXF") do (
ren "%%F" "%%C"
if exist "Y:\Dest\%%B" (move "%%C" "Y:\Dest\%%B") else (move "%%C" "Y:\Dest\Unknown")
)
)else (move "%%C" "Y:\Dest\Unknown")
)
endlocal
If I add the if condition the script is not working, without the if condition it's working.
ABC_TEST1.txt should be moved to Y:\Dest\ABC, EFG_TEST1.txt should be moved to Y:\Dest\EFG
It's not moving the XYZ.txt, GHI_TEST1.txt to Y:\Dest\Unkonow.
Itemized adjustments
Remove the ren command since it's causing the script to fail.
Add an exist check inside the for loop to check if the destination folder exists.
If it exists it will move the files to that folder
If it does not it will move the files to the Unknown folder
This should move all .txt files in the current directory to the appropriate subfolder in the destination folder based on the prefix of the file name before the underscore.
Batch
setlocal EnableDelayedExpansion
for %%F in (*.txt) do (
set FileName=%%F
echo %%F
for /F "tokens=1* delims=_" %%B in ("%%~nXF") do (
if exist "Y:\Dest\%%B" (
move "%%F" "Y:\Dest\%%B"
) else (
move "%%F" "Y:\Dest\Unknown"
)
)
)
endlocal

Bat file to compare folders

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.

Batch Script to find a files inside all Sub Folders

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

How to batch file to create folder from part of file name and move files

Batch file to create folder from part of file name and move files.
I have lots of files that need to be in their own sub folder.
They are in this format:
Example
123456-ABC-XYZ
123456-DFG-XYZ
123456-HIJ-XYZ
Where I would like to create a sub folder and move the files to it by characters 8-10
or by 12-14
Used this as a base that I found on this site posted by Magoo
and it works to do it by the 1st set of characters (h:\1AAATEST = my test folder
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*_*-*-* *.*"'
DO
(MD %%b MOVE "%%a %%b" .\%%b\)
POPD
GOTO :EOF
[Editing note from Magoo:
This is incorrect. Here's what I posted:
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*_*-*-* *.*"'
) DO (
ECHO MD %%a
ECHO MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
reference: Question from nearly a year ago
Notice that there are major difference beyond the change of source directory and omission of the echo keyword as narrated. OP's syntax in this question simply won't work.
]
I modified it with the same results as the original
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*-*-*.*"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
Then again, and it created 4 folders from this file name
LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1
But didn't move any files.
1st folder = -
2nd = 11-18-10_1
3rd = Diagnostic
4th = Topography
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*-*-*.*"'
) DO (
MD %%b
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
I came up with the following:
#echo off
for /f "tokens=1-3* delims=-," %%a in ('dir /b /a-d "*-*-*"') do (
(md "%%~nb" 2>nul)
(MOVE "%%a-%%b-%%c" "%%b"))
POPD
GOTO :EOF
It works for making directory and moving files for the 1st and 2nd section of the file name,
by changing the md to a & the move target dir to a for 1st part of file name &
by changing the md to b & the move target dir to b for 2nd part of file name &
BUT changing the md to c & the move target dir to c for 3rd part of file name &
DOESN"T work. It creates the correct dir, and it doesn't move the files but it shortens the filenamnes to XYZ.
Suggestions would be appreciated.
I received an angry telephone call one day from someone who'd used a library I published and it had a major fault. In reality, it had been modified and republished by someone with no note as to who, when or why in the library source. That was the last time I ever published sourcecode for my commercial work.
The problem you are encountering is very simply explained.
The for /f examines LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1 and assigns LAST_FIRST_7-24-1936 to %%a and Diagnostic - Topography 11-18-10_1 to %%b
In the code as you have modified it, you are then executing MD %%b which will be interpreted as
MD Diagnostic - Topography 11-18-10_1
so batch creates those four directories, as requested.
Then the move is encountered. The destination directory is .\%%a\ which will attempt to move the file to .\LAST_FIRST_7-24-1936\ - a directory which is unlikely to exist.
So - to fix the problem, try replacing MD %%b with MD %%a as was specified in the original code.
As for your requirement for 123456-ABC-XYZ, then try
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2,*" %%a IN (
'dir /b /a-d "*-*-*"'
) DO (
MD %%b
MOVE "%%a-%%b-%%c" .\%%b\
)
POPD
GOTO :EOF
(this version to move to the directory with the middle block of characters - to move to the end-block, replace %%b with %%c in both the MD command and the destination of the move command)
BTW - it's very likely that the md will complain about attempting to create an existing directory. You can shut it up by appending 2>nul to the end of the md command.

Batch script - concatenate sequential numbers on files to keep the names unique

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.

Resources