Moving files with a batch file - batch-file

I have 30 folders, one for each travel group with 10 subfolders each, each subfolder corresponds to an activity.Some pictures dont match with the group, so i have a list of the pictures that dont match.
Im using this batch to move the jpgs from the folders but it's putting them all together. is there any way that the batch creates a folder with the name of the folder it's been moved from?
for /r "originfolder" %%# in (*) do findstr "%%~nx#" "filelist.txt"&&move "%%#" "destinyfolder"
PAUSE

You'll need a few more lines. There are a few different ways, this being one of them.
#echo off
setlocal enabledelayedexpansion
pushd "originfolder"
for /F "usebackq delims=" %%a in ("filelist.txt") do for /F "delims=" %%i in ('dir /b /s "%%a" 2^>nul') do (
set "filepath=%%~dpi"
set "filepath=!filepath:~0,-1!"
for %%f in (!filepath!) do set "destpath=%%~nxf"
mkdir "C:\Full path to\destinypath\!destpath!"
move "%%~a" "C:\Full path to\destinypath\!destpath!"
)
popd
simply get the path of the file, then use the last name of the path (folder where file exists) and create that folder name in your destination path, before moving the file.
Note, this is untested code, so please use a test scenario before attempting this in production.

Related

Batch create folders based on part of file name, then move them into that folder

Similiar problem as previously solved question here - Batch create folders based on part of file name and move files into that folder
QUESTION: I have 15k files that I want to extract the first word from file name to create a folder, then move all those respective files into the folder.
This is driving me crazy trying to figure out!!!
**EXAMPLE: **Using the files below, I want all files beginning with PTC to move into a new folder called PTC, then the same process with files beginning with PRIORITY.
PTC-Affiliate Digital Asset Refresh (160x600)
PTC-Affiliate Digital Asset Refresh (160x600)2
PTC-Affiliate Digital Asset Refresh (160x600)3
PRIORITY-Affiliate Digital Asset Refresh (160x600)
PRIORITY-Affiliate Digital Asset Refresh (160x600)2
PRIORITY-Affiliate Digital Asset Refresh (160x600)3
I tried revising existing code (below), but am running into 2 issues:
File names use spaces, not underscore - how do I denote that in the code?
When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.
CODE I AM TRYING TO EDIT TO DO WHAT I WANT:
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\[directory\]"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=-" %%a IN ('dir /b /a-d "*\_*_\*_*\_*"') DO (
MD %%a 2\>nul
MOVE "%%a\_%%b\_%%c\_%%d\_%%e" .%%a\\ 2\>nul
)
POPD
GOTO :EOF
Any help or suggestions would be VERY welcomed.
Thank you!
I tried revising existing code (below), but am running into 2 issues:
File names use spaces, not underscore - how do I denote that in the code?
When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.
I am not able to solve for either of these issues.
Maybe your approach of focusing on old code is not allowing you to focus on the task at hand :)
You really only care about the first word, delimited by -. So split only that, then move all files starting with the first word once off:
#echo off
SET "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "delims=-" %%i in ('dir /b /a-d "*-*"') do (
mkdir "%%~i">nul 2>&1
move %%i* "%%~i">nul 2>&1
)
popd
however, there is a chance (though you did not specify this) that there are other files that you might want to skip in the directory, then might do:
#echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for %%a in ("*-*(*x*)*") do for /f "delims=-" %%i in ('dir /b /a-d "%%~a"') do mkdir "%%~i">nul 2>&1 && move "%%a" "%%~i"
popd
and if you feel like some regex and be more specific with your query, then findstr will help by enforcing wildcards, spaces and/or alphabetical or numeric character ranges as in the extract from below. findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$" :
#echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "tokens=1,*delims=-" %%a in ('dir /b /a-d ^| findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$"') do (
mkdir "%%~a">nul 2>&1
move "%%a-%%b" "%%~a"
)
popd

XCOPY copying files based on ID (With network paths)

I have created a batch-file which reads an ID from a CSV file and then searches for files with that ID contained within the title of log files created or modified within a specific date range to move them into a network folder.
At this moment, it seems the code is working (+-) but it is copying more files than it should, without looking at the ID.
It works on my local machine, so it seems the problem is regarding the network paths and it shows the UNC not supported path.
cd \\10.XX.ZZ.YY\ServersFiles
ECHO Will start reading the log files
for /f "usebackq tokens=4 delims=," %%a in ("\\10.XX.ZZ.YY\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv") do (
for /r %%i in (*%%a*.log) do xcopy /Y /D:%date_arg% "%%i" "\\10.XX.ZZ.YY\Logs\"
)
This is what I'm getting right now
If UNC paths aren't allowed, you could try using PushD instead of CD, which will create a temporary drive mapping to the network location. Throughout the rest of your script you could then use relative paths to the required locations.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
REM Your %date_arg% variable should be defined somewhere around here.
PushD \\10.XX.ZZ.YY\ServersFiles 2>NUL || Exit /B
If Not Exist "..\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv" Exit /B
Echo Will start reading the log files
For /F "UseBackQ Tokens=4 Delims=," %%A In (
"..\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv")Do For /R %%B In (
"*%%A*.log")Do XCopy "%%B" "..\Logs\" /D:%date_arg% /Y
PopD
EndLocal
Exit /B

Automatically Create Folders Based on String in Filename

I need a batch file to create a process for a large list (2k) of TIF files in a local folder directory.
The filename structure is, for example: 12345_1.tif.
I need the batch to:
1 - Create a folder name based on the number(s) after the underscore, as this is the only constant in the naming. The folders only based on this sole number.
2 - Copy and move the file into the newly created folder.
In the example above, the batch would create a folder called 1 and then move the file 12345_1.tif into that folder. If it found another file such as 54321_1.tif, that file would also be moved to the "1" folder. In my files, the numbers after the _ range from 1 through 77, and there could multiple files that share the same number after the _.
I've observed some similar scripts online, but I need help to modify my requirement. Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
It seems you didn't understood my comment, so I post it here as an answer with complete code:
#echo off
for /F "tokens=1,2 delims=_." %%a in ('dir /B *.tif') do (
md "%%b" 2>NUL
move "%%a_%%b.tif" "%%b"
)

Copy first file for multiple folders to some another location

I have about 1000 folders on my hard drive. Each folder contains about 100 .jpg files. Now I need to copy from each folder "page (1).jpg" file in to some specific folder(each folder contain "page (1).jpg" file). And one more thing... On the end, after copying, each .jpg file from specific folder need to have name like folder from which it was copied.
How to do this on easiest way. Maybe with some batch file or something else...
not tested (you need to change the names of root_dir and target_dir):
#echo off
set "target_dir=E:\scriptests\redirection"
set "root_dir=E:\scriptests\labels"
pushd %root_dir%
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b /s "page ?1?.jpg"') do (
set "fdir=%%~dpa"
set stripped=!fdir:~0,-1!
for %%# in ("!stripped!") do (
echo %%~n#
set "dir_name=%%~n#"
)
copy /y "%%~fa" "%target_dir%\!dir_name!.jpg" >nul 2>nul
)
endlocal
popd
pause

DOS BATCH: How to compare the existence of 2 files with the same name in 2 separate folders?

I want to check all the files in one folder to see if they exist in another folder. When a file is found in the first folder that doesn't exist in the second folder I want that file to be deleted.
Is this possible?
Edit path info as appropriate (or incorprate batch arguments %1 %2), and remove ECHO that precedes DEL once you confirm you are getting the correct results.
#echo off
setlocal
set "dir1=."
set "dir2=d1"
set tempFile="%temp%\exclude%random%.txt"
dir /b "%dir2%" >%tempFile%
for /f "eol=: delims=" %%F in ('dir /b /a-d "%dir1%" ^| findstr /vixg:%tempFile%') do echo del "%dir1%\%%F"
del %tempFile%
Note - This solution is simply comparing names. Two completely different files will be considered the same if they have the same name.

Resources