Batch File - Using List, move files from folders - batch-file

I have 3 folders located in a INPUT folder.
Also have 3 folders in a OUTPUT folder like so...
"INPUT"
-Folder1
-Folder2
-Folder3
"OUTPUT"
-Folder1
-Folder2
-Folder3
I have a file listing (list.txt) from INPUT\Folder1.
I need it to read each line from the list.txt, and if exists as a file, then move those to the OUTPUT destination folders respectfully.
Here's what I have right now, but it copies all files over to the destination folders and not just what is in the list.txt:
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder1
%CD%\OUTPUT\folder1
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder2
%CD%\OUTPUT\folder2
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder3
%CD%\OUTPUT\folder3

I figured it out!
Decided to try xcopy and throw in the "delims=*" for spaces and special characters in the filenames and it worked!
For /f "delims=*" %%f in (list.txt) do xcopy "%CD%\INPUT\folder1\%%f"
%CD%\OUTPUT\folder1
So now it will only copy over the files listed in the list.txt.

Related

Script batch copy from subfolder to main folder filtering extension

I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied

Handling space in the file name, FOR loop, batch

I am trying to find all the MP3 files in a given directory, then export it to a file, before trying to use the contents of the file as an input.
All the instances of file copy are working fine, except for the file locations which contain spaces. How should I address this in my current code. Please refer to the screenshot below
Contents of my MP3_Location.txt file are:
C:\Test\asdad.MP3
C:\Test\New folder\werwer.MP3
C:\Test\OneDrive - Backup\asdasdasdad.MP3
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files based on the previous Output
FOR /F %%G IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy
"%%G" C:\Software\MP3\ /Y
Edit 1: Trying to use Delims now, as suggested (perhaps not using it correctly)
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files
FOR /F %%G "tokens=* delims=" IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy "%%G" C:\Software\MP3\ /Y
Used the command
FOR /F "delims=" %%G IN

How to attach the zipped folder names to all the files inside it in batch file

I have a folder in a directory which has hundreds of zipped folders in it and also many files inside it.for example ,
Folder
cs2-20150613-6014-0000-201071.zip
cs2-20150613-abc.bin
cs2-20150613-xyz.bin
cs2-20150614-6014-0000-201066.zip
cs2-20150614-abc.bin
cs2-20150614-xyz.bin
I want to attach the last 6 digits of the folder name to all the files after extracting,
cs2-20150613-abc.bin-201071
cs2-20150613-xyz.bin-201071
Can anyone suggest me how it can be done in batch file
#echo off
for /F "tokens=1,2,5 delims=-." %%a in ('dir /B *.zip') do (
ren %%a-%%b-*.bin %%a-%%b-*.bin.%%c
)
This code assume that the format shown is fixed: cs2-YYYYMMDD-xxxx-xxxx-######.zip. If this format may change, an adjustment in the code is needed.
Here's a short script I wrote that should do what you need
#echo off & setlocal enabledelayedexpansion
for %%a in (*.zip) do (
7z x -o"%temp%\%%~na" "%%a" > Nul <&2
set "suffix=%%~na"
set "suffix=!suffix:~-6!"
for %%b in (%temp%\%%~na\*) do move /Y "%%b" "%cd%\%%~nxb-!suffix!" > Nul <&2
rd /s /q "%temp%\%%~na"
)
It requires that you have 7z on your path for extracting the compressed files for .zip folders.
The suffix added it the extracted files is always the 6 characters before the extension for the .zip file, ignoring weather or not they're digits.
This code utilizes the idiom > Nul <&2 to redirect all output (stdout and stderr) to the Nul file, effectively silencing the program.

List filenames and folder paths on csv using batch file

I have a batch file that lists the full file directories (including filename at the end) onto a csv file. I need it to produce this, but also just the filename in a separate column. I would like it so that the format is Filename in first column (including extension) and full file directory in second column. The batch file I currently have is:
dir C:\Users\Administrator\Desktop\test\Files\*.tif /b /s >>
C:\Users\Administrator\Desktop\test\Output.csv
EDIT: I forgot to mention the 'Files' folder contains many subfolders so I need it to process all files from all these subfolders.
Thanks in advance.
You just need to do this:
#echo off
setlocal
set "in=C:\Users\Administrator\Desktop\test\Files\*.tif"
set "out=C:\Users\Administrator\Desktop\test\Output.csv"
if not exist "%out%" type nul>"%out%"
for /f "delims=" %%a in ('dir /b/s %in%') do (
>>%out% echo "%%~nxa","%%a"
)
Iterate and write (help for for info on the %~ variable modifiers):
#echo off
cd C:\Users\Administrator\Desktop\test\Files
for %%f in (*.tif) do echo "%%~nxf","%%~dpf" >> Output.csv

Batch script to copy multiple files (based on string in filename) from all folders under a directory to another folder

Hi I am looking for a (simple) batch script for Windows XP to copy pdf files with different filenames but having the same reference numbers in their filenames to a specific folder in a different drive.
E.g.
Copy all pdf's with reference number 111 to folder "test"
Source files in F drive
F:\folder 1\filename 1_111.pdf
F:\folder 1\folder 2\filename 2_111.pdf
Destination folder in C drive
C:\test\
I am a novice, so thank you in advance for your help.
Regards,
Olive
This is another option:
#echo off
for /f "delims=" %%a in ('xcopy /l /e /y "F:\folder 1\*111*.pdf" "c:\test\" ^|find ":"') do copy "%%a" "c:\test"

Resources