In Batch, how to copy files within a user specified range - batch-file

I have a scenario where I need to copy files from five subdirectories to the directory where the batch file exists. These files are sequential in name in each subdirectory. For example:
FolderA:
A001.txt
A002.txt
A003.txt
A004.txt
FolderB
B001.txt
B002.txt
B003.txt
B004.txt
FolderC
C001.txt
C002.txt
C003.txt
C004.txt
and so on. What I want to be able to do is have a user input where I am able to copy the files from each directory that end in 003 and 004 to a new directory. I have more of the batch that does some data scrubbing from there with the copied files. My problem is getting the files copied based on my number range.
I am able to add a user input to allow me to select all file that end in 003 and copy them, but then I have to run it again to get all files that end in 004. I have close to 25,000 files to search through, and I have completed data mining some. I need to copy them in from a range.
Here is what I have that works to allow me to input and copy a single file set
set fname=
set /P "FileNumber=Please enter File Number: "
if defined FileNumber for /R "\client\A001\M0093" %%I in ("*%FileNumber%*.xml") do (copy /Y "%%I" "\Extraction\Files\"
set "fname=%%~I"
)
if defined fname (
cls
#echo off
copy "Files\*.txt" Data.txt
Any help would be greatly appreciated.

I am posting from my mobile, so none of this has been tested:
#echo off
set /P "begin=beginning number"
set /P "end=Ending number"
Pushd "client\A001\M0093"
For /l %%i in (%begin%,1,%end%) do for /R %%b in ("*%%i*.xml") do copy /Y "%%~b" "%~dp0"
Also, this assumes the files are copied to the path where the batch-file is located, as you have mentioned.

Related

How to rename folders based on text file where names are not in the same order

I have the following code that works, but I still need more functionality.
#echo off
setlocal EnableDelayedExpansion
< albumsids.txt (
for /F "delims=" %%a in ('dir /B *.*') do (
set /P newalbumsids=
ren "%%~Fa" "!newalbumsids!%%~Xa"
)
)
I have several folders like:
Folder
Folder name 1
Folder name 2
Folder name 3
Folder name 4
Folder name 5
Folder name 6
and an .txt file with the name I wish to rename my folders:
The .txt file I fill manually the new names
albumsids.txt
6544233_Folder
1234233_Folder_name_1
6575670_Folder_name_2
4756722_Folder_name_3
3375673_Folder_name_4
6575675_Folder_name_5
8575677_Folder_name_6
the list continue..
My problem is that not all the time in .txt the names are in order and I have hundreds of folders.
Most of the time are mixed:
albumsids.txt
1234233_Folder_name_5
6575670_Folder_name_3
6544233_Folder
475672_Folder_name_6
3375673_Folder_name_2
6575675_Folder_name_1
8575677_Folder_name_3
the list continue..
I need it to load the entire text file, search in .txt for the partial name of the initial folder Folder name and then copy the entire title with which to rename the folder that match.
Basically the new names insterted manually by me contain an "unique ID" and "_" used as delimiter
all the time the structure will be:
Uniqueid_FOLDER_NAME
Uniqueid_FOLDER
Folder can be from 1 word or more,
so
Folder will become 1234233_Folder
Folder Name 5 will become 1234233_Folder_name_5
Folder Name 1 will become 1234233_Folder_name_1
Folder Name 3 will become 1234233_Folder_name_3
and so on...
thank you for helping me
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename 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"
SET "filename1=%sourcedir%\q73613649.txt"
FOR /f "usebackqtokens=1*delims=_" %%b IN ("%filename1%") DO (
SET "#=%%c"
SET "#=!#:_= !"
ECHO REN "%sourcedir%\!#!" "%%b_%%c"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Simply, split the new name into 2 parts using _ as a delimiter, replace the underscores from the second part with spaces, then rename the directory. If the directory may not exist, either bear with the error message or dump it by appending 2>nul to the ren line.

How can I improve a batch file with a conditional who copy a jpg file refferring to a specific extension and to a specific database?

I created a .bat file with the following code in order to copy a random file from a directory A-POOL FOLDER into a directory B-FOLDER (with a fixed file name video.mp4). So, in the directory A there is a pool of .mp4 file, and in the directory B there is the the file video.mp4 (same name, but different video every time I execute my batch-file).
That's a code who do this. It perfectly works.
#echo off
setlocal EnableDelayedExpansion
cd C:\Users\aless\Desktop\example\A-POOL
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" C:\Users\aless\Desktop\example\B-FOLDER\video.mp4
FOR %%G IN ("!file[%rand%]!") DO >"C:\Users\aless\Desktop\example\B-FOLDER\title.txt" ECHO %%~nG
Now the question (different from last time):
In the directory A-POOL I have a lot of .mp4 files. I changed manually the .mp4 extension of every file with a code who rappresent a food.
For example: Filename.pizza, Filename2.pizza, Filename.pasta, Filename200.pasta, Filename.cheeseburger etc...
I created another folder named "FOOD-DATABASE" (in C:\Users\aless\Desktop\FOOD-DATABASE) who contains a big database of images in .jpg extension of the foods. The file NAMES of the food database are equal to the file EXTENSIONS of the files contained in A-POOL folder
Content of FOOD DATABASE folder:
(pasta.jpg, pizza,jpg, cheeseburger.jpg... etc..).
So I need to add to the code a string/conditional who execute e command who say:
If the picked random File have the extension .pizza COPY pizza.jpg from C:\Users\aless\Desktop\FOOD-DATABASE into C:\Users\aless\Desktop\B-FOLDER\FOOD.JPG (fixed name file)
Actually I don't want to add this strings for every food type... in few word the code strings have to read the extension of the picked random file and copy from C:\Users\aless\Desktop\FOOD-DATABASE to C:\Users\aless\Desktop...B FOLDER\FOOD.JPG (fixed name file) the file with the same name to the extension of the random file.
It's a mess... I know ;) Thanks for your help!
As mentioned in the comments, we know that the extension of the one file is the name of another file. so these variable expansions are important:
%%~nG
%%~xG
The last for loop sets a variable %food% from expanded %%~xG which is the extension of the file and using a random example would be .pizza We just need to get rid of the . and you have pizza. We do this by %food:~1.
#echo off
setlocal EnableDelayedExpansion
pushd "%userprofile%\Desktop\example\A-POOL"
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" "..\B-FOLDER\video.mp4" /Y
for %%G IN ("!file[%rand%]!") DO (
echo %%~nG > "..\B-FOLDER\title.txt"
set "food=%%~xG"
)
copy "..\FOOD-DATABASE\%food:~1%.jpg" "..\B-FOLDER\food.jpg" /Y
popd
Notes!
I am using pushd instead of cd and using relative path, not full paths as we are working in the same folder structure.
I can make out that you are running this on your own profile, so I replaced C:\users\<username> with %userprofile% in the event someone else wants to run it from a similar folder, their <username> will be different.
popd at the bottom of the script simply changes back to the previous working directory you were in before the last pushd. it is not needed for this particular script, but adding it so you can learn about it compared to pushd
All of the items I modified has some good help topics, you can find help topics on cmd by simply running the command name with an appended /? for instance for /?, set /? etc.

How to read path from textfile?

I have a .txt file with multiple lines. Each line contain a path to a folder on my network drive/computer:
[NetworkDrive_PathToFolder1]
[Computer_PathToFolder1]
[NetworkDrive_PathToFolder2]
[Computer_PathToFolder2]
[NetworkDrive_PathToFolder3]
[Computer_PathToFolder3]
etc...
The idea is to have a .bat file that will read line 1 and 2 and copy files in folder [NetworkDrive_PathToFolder1] into folder [Computer_PathToFolder1], then read line 3 and 4 and copy files in folder [NetworkDrive_PathToFolder2] into folder [Computer_PathToFolder2], etc.
This will enable me to add/remove paths without need of altering the code.
I'm not sure how the .txt file should look like. Maybe it's better to use a xml file?
This is for a Windows 10 computer with a network drive connected.
When I start my computer I want to automatically update some specific files from my network drive to a local folder on my computer.
To map the network drive I'll prefer to use PUSHD and POPD instead of NET USE since I don't want the network drive to be permanently mapped.
Today I have a .txt file with paths to the folders in the network drive:
[NetworkDrive_PathToFolder1]
[NetworkDrive_PathToFolder2]
[NetworkDrive_PathToFolder3]
etc...
And the path to the folder where the files are copied to are in the .bat file.
But then everything is just copied into the same folder at my computer.
FOR /F %%i IN (C:\UpdateFilesFromNetworkPaths.txt) DO (
NET USE L: /delete
NET USE L: %%i
xcopy L:\*.txt C:\FilesFromNetwork\ /d /y
)
Result from this code:
Every .txt files that doesn't exist in the folder [C:\FilesFromNetwork] will be copied from the paths in the textfile [UpdateFilesFromNetworkPaths.txt].
If a file exist, copy the file if it's a newer version.
So based on your post, it looks like you're trying to copy files based on paths in a text file. All groups of two (1-2, 3-4, 5-6) contain source and destination paths. This problem can be solved by a simple for loop and a simple counter.
Assuming your text file (.txt) is formatted in my example bellow:
C:\PathToNetworkDriveFolder1
C:\PathToComputerFolder1
C:\PathToNetworkDriveFolder2
C:\PathToComputerFolder2
C:\PathToNetworkDriveFolder3
C:\PathToComputerFolder3
Bellow will be the solution script:
#ECHO OFF
#Setlocal EnableDelayedExpansion
Set "Count=1"
Rem | Read The Text File Line By Line
FOR /F "tokens=*" %%A in ('Type "list.txt"') DO (
Rem | Grabbed First Line
IF "!Count!"=="1" (
Rem | Add One To Count
Set /a "Count=!Count!+1"
Rem | Save Current Variable
Set "NetworkDrivePath=%%A"
Rem | Grabbed Second Line
) else (
Rem | Subtract One To Count
Set /a "Count=!Count!-1"
Rem | Save Current Variable
Set "ComputerDrivePath=%%A"
Rem | Copy Files
Echo "!NetworkDrivePath!" "!ComputerDrivePath!"
)
)
Goto :EOF
Keep in mind my example uses list.txt. Feel free to change that to your example or your usage. If you have multiple text files you want to use your can simply add another for loop at the beginning to search for all *.txt file names.
For all my scripts, I put in rem comments to help you follow along in what each process is doing. For help on any of the commands do the following in a new window:
set /?
for /?
if /?
ren /?
So on.

Batch create folders from files with similar beginning but different ending

I have a lof of files with similar beginning names, but ending different. I have to create a folder for every "series" of files, which means all files whose names begin with the same 7 characers.
See image for visualization:
The code I have right now is
for /F "Tokens=*" %%i in ('Dir /B *.jpg') do md "%%~ni"|copy "%%i" "%%~ni"
Which just creates a folder for every file and puts it inside.
Unfortunately I don't have enough knowledge of this language and just googling it only made me learn how to create a substring SET _result=%_test:~0,7% but not how to create folders with that.
I'm sure it's just a short sequence, could someone help me with that?
This may not be the most efficient way of doing this, but it should help you. The below batch script takes an input folder and output folder as inputs from user. It then iterates through all the files in the input folder and copies them to output folder under a sub-folder with first 7 characters of the filename.
#echo OFF
setlocal ENABLEDELAYEDEXPANSION
REM Get input directory from user
set /p INPUT_DIR=Please enter full path to directory with files:
REM Get ouput directory from user
set /p OUTPUT_DIR=Please enter full path to output directory:
REM Iterate through files and create necessary folders/copy files
for /f %%f in ('dir /b %INPUT_DIR%') do (
set fullname=%%f
set trimmedname=!fullname:~0,7!
set fullpath=%OUTPUT_DIR%\!trimmedname!
if not exist !fullpath! md !fullpath!
copy %INPUT_DIR%\%%f !fullpath!
)
Here's a sample output that shows the processing done by the script:

Batch Script Move file to directory based on its current directory

I'm attempting to sort a lot of files based on the current location of the file e.g.:
File 1 is located at C:\Work\Movies\Subs\Subtitle.txt
File 2 is located at C:\Work\Movies\Subs\Special\Subtitle.txt
File 3 is located at C:\Work\MoviesSpanish\Subs\Subtitle.txt
I'm trying to move the files like so:
File 1 to C:\Work\InProgress\Movies\Subs\Subtitle.txt
File 2 to C:\Work\InProgress\Movies\Subs\Special\Subtitle.txt
File 3 to C:\Work\InProgress\MoviesSpanish\Subs\Subtitle.txt
The Batch Script is to be located in C:\Work\MoveFile.bat
There are away more files then I listed above. Just for an estimate I would say around 300-500 per folder and there's a lot more subdirectories (e.g. .\Subs\01\ all the way up to .\Subs\300\ and they each contain a bunch of text files). I need to move all of the .txt files from their current locations to a new folder in C:\Work\ while retaining the rest of the directory location. So they get moved to C:\Work\[New Folder]\[Rest of Original Location]
I want to do this in batch but I'm not sure where to start. I already have the following code, which deletes files that don't contain a specific string:
for /r %%Z in (*.txt) do (
SET "count="
for /F "usebackq delims=," %%A in ("%%Z") do (
if /i "%%A"=="LN" set count=1
)
if not defined count echo del "%%Z"
if not defined count del "%%Z"
if defined count move "%%Z"
echo %count%
echo %%Z
)
But I'm not sure how to obtain the correct directory to move them into. I was thinking about for loop that reads the directory string of the file in question and uses delims=/ but it kept reading the text file rather then the path (and when I didn't use quotes on %%Z, it decided it couldn't find the file).
This is untested - it uses robocopy to move the *.txt files into a new location which is outside the source folder (because of cyclic copy restrictions) and then moves them back to the new "C:\Work\InProgress" folder.
If %temp% and c:\work are on the same drive then it should be swift.
Change c:\work in both places to a test folder with a few copies of your files and test it.
#echo off
robocopy "C:\Work" "%temp%\temporarymoviefolder" *.txt /s /mov
robocopy "%temp%\temporarymoviefolder" "C:\Work\InProgress" *.txt /s /mov
pause

Resources