for /L %%f in (1,1,10) do copy File1.txt %%f.txt
this code does the job very well, but I'm trying to understand how to change it to make it read
subfolders, so that I don't have to keep moving the batch file to every folder
I saw this, but not really sure how to put it together
#echo off
SET "sourcedir=C:\Users\user\Desktop\Main\Original"
SET "destdir=C:\Users\user\Desktop\Main\Copied"
for /L %%f in ('dir /s 1,1,10) do copy *.txt %%f.txt
in the section - copy *.txt %%f - I put a * so that it can only look for .txt files, but this action
slows down the coping and then stops working.
I know my code is a mess, just trying to put something together
I have many Subfolders and each folder has 1 txt file in it with all random names
and I need to make multiple copies of each file.txt in each folder
I have so many subfolders that it would literally take me months of time to get all files copied
and by then I would have many more new files to work on
so getting this copier to read Subfolders is like top priority for me.
I would like help putting this together and then explaining how it links
because I'm interested in applying the Set and dir to other batch file I have
Please any details on this will be much appreciated
I was told to look into xcopy and robocopy, but I have no idea were to add a counter
#echo off
for /1 %f in (1,1,10) do xcopy "C:\Sources" "C:\Target" /c /d /i /y
exit
So I have this that reads from source and dumps in main folder where the batch is
Option 1)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\*.txt" %%f.txt
Option 2)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\" "C:\Destination Address\ %%f.txt"
The thing I don't like is that is asks me a question
and I have over 10,000 txt files, I can't sit here and press F for Filename
10,000 times, can we disable that
OK, so I got this working
all I need help with is were to add this /c /d /i /y
I am still trying to get it to read Subfolders with the batch sitting
in the main folder and me not having to move files back and forth
Option 3)
for /L %%f in (1,1,110) do copy "C:\Source Address\*.txt" %%f.txt`
This works well with the Source and the wild card #magoo told me to add
the Source before the .txt file
But with this code I would still have to open hundreds of folders and move
the file to the source run the copier and then move back all copied files
Still need help with Subfolders
for /L %%f in (1,1,10) do copy File1.txt %%f.txt
will vary %%f from 1 (the first number in the parenthesised list) to 10 (the last) in steps of 1 (the middle) and will therefore copy file1.txt to 10 separate files, 1.txt to 10.txt.
Since there are no paths specified, the copy will be performed from file1.txt in the current directory to 1.txt .. 10.txt in the current directory.
If you were to put your batch file in any directory that is mentioned in the path variable (just execute path at the prompt to show it) then no matter where the current directory is, you could just use that batch filename to execute the batch, and the files would be created by copying file1.txt in the now-current directory to 1.txt .. 10.txt in the now-current directory - if file1.txt exists in the now-current directory, and if not, it will generate error messages reporting that the source file is missing.
If you were to replace file1.txt in the batch with "x:\wherever\file1.txt" then the file would be copied specifically from the file "x:\wherever\file1.txt" regardless of whether file1.txt exists in the now-current directory. (And please get used to "quoting file or pathnames" as it will avoid a whole slough of problems when you tackle names containing spaces and some other special characters).
I have no idea how for /L %%f in ('dir /s 1,1,10) do is supposed to work since according to Hoyle, the first element in the parenthesised list should be a number. I'd suggest that we have a small transcription problem here.
Th slower and slower problem - yes, understandable. You are creating more and more .txt files, and copying all of them to a new destination...
Perhaps referring to This response and question might show how to solve your subdirectory-scan issue.
Related
I am looking to write a script to help with my research. I need to be able to move files from multiple subdirectories to a single destination directory. I have a list of files to be moved, but have no experience writing anything like this. Did some digging around and botched together this
#echo OFF
CHCP 65001 >NUL
FOR /F "usebackq delims=" %%I In ("F:\Coyote Pics\Final sorted pictures\COYCAM1.txt") DO (
xcopy /s "F:\camelot\Datasets\default\Media\%%I" "F:\Coyote Pics\Final sorted pictures\%%I*"
)
PAUSE
which depends on making a txt file of the file paths to be copied. However, when I run the script, I get "file not found". Not sure if this is the best way to do this or if there is an easier way.
For reference, here is an example of a list of files that I need to copy into a single destination folder.
F:\camelot\Datasets\default\Media\bd\bdb71a66-4a70-4f34-adcb-2a4792d7a736.jpg
F:\camelot\Datasets\default\Media\22\22976f1a-b718-43ac-89b1-5db2815f1a38.jpg
F:\camelot\Datasets\default\Media\01\013a0efe-12e0-40fd-9057-bec92ead8865.jpg
F:\camelot\Datasets\default\Media\47\47543fc8-7ba8-4989-b5e1-1e57052b5fd4.jpg
F:\camelot\Datasets\default\Media\08\08d84e39-7458-4e8e-88bd-b443b8e419f2.jpg
F:\camelot\Datasets\default\Media\8e\8e814897-2f6f-45d4-9be3-77d41c4fc836.jpg
I am trying to replace over 400 audio files, in just about as many sub-folders, with a updated format required by the program. It's the same file in each location but its how they require it for their xml file. I found several similar post, replacing files in multiple sub-directories like this one Replacing a file into multiple folders/subdirectories this I assumed would meet my needs.
I want to find all AR33.mp3 files in SFX'x folders, and replace with the new required AR33.wem my issue is as follows:
FOR /R C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX %%I IN (AR33.mp3) DO echo COPY /Y C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem %%~fI
This does the reverse and copies the file I want to overwrite, ar33.mp3, all over the folder structure in every folder where there shouldn't be any. but it does say 1 file copied after each entry.
(AR33) creates extension-less copies. adding (*AR33.mp3) does nothing, same with (*AR33.mp3 *) does nothing, even with "1 item copied" pointed out in the console. At this point I'm trying what I can to get this to work randomly doing stuff. Changing COPY to replace give a invalid switch -/y. Am I using the wrong "script" to do what I want? Because even changing one of the AR33.mp3 over to a .wem didn't see any change after replacing the line .mp3 to .wem. I assume I need to fix what its searching for and what its copying.
Updated bat:
FOR /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I IN (AR33.mp3) DO echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~fI"
Output sample:
C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted>echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
You want to replace .mp3 with .wem as well. For now you are simply copying the .wem file and naming it with a .mp3 extension. So we could just rename the .mp3, then copy the .wem file to it.:
#echo off
for /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I in (*AR33.mp3) do (
echo ren "%%~fI" "%%~nI.wem"
echo copy /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~dpnI.wem"
)
Please take note before continuing. The above will only echho the result, once you are happy that it does what you want, then only remove `echo from both strings in the code block.
It is also always a good idea to backup files before running a big copy process like this.
I am still a beginner to batch scripting so I would appreciate more explanation than usual to help me if at all possible.
Purpose:
I wrote this script to try to move files based on whether they have a folder's name within their name. For example, I have a folder named "cow" and a file named "cow_5234.txt", the file would be moved into the folder. The purpose of this is to move files with company names to a company folder from a messy folder full of files so I don't think a file going to the wrong folder will be much of a problem.
Problem:
The problem I am having is that many of the files that are supposed to be transferred to folders are being misplaced. I have around 3000 files in a folder along with 300 folders and this script. After I run it, only around 100 files get placed into the correct folders, while 900 are left behind (because they don't have a folder name within them) while the rest just disappear.
What I have tried to do:
I have gone through and checked the original list of folder names, which is all correct. Then I modified the script to output the filename to a .txt file instead of move it. Instead of getting a reasonable amount of files, there are 4000 files (I checked the line count). Some of these files don't have a folder name in them. I then took a subset of around 200 files and 20 folders and ran the script with them. For some reason, only 80 files remained, 1 was placed into a folder, although it didn't have a single similar character as the folder, and the rest just disappeared. I'm not sure what is happening at all. Here is the code:
#echo off
dir /a:d /b > list.txt
for /F %%i in (list.txt) do call :process %%i
goto :eof
:process
set "search=%1"
for /f "eol=: delims=" %%F in ('dir /A:-D /b^|find "%search%"') do move /Y "%%F" "%search%"
goto :eof
Edit 1:
As recommended in the comments, I have added a "goto :eof" to the end of the block of code, however, the problem still remains.
Edit 2:
I didn't add a "goto :eof" to the end of the main function. Thanks for clarifying!
So I have the batch file below.
#ECHO OFF
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
for %%i in (%*) do (
move "%%~i" "C:\%FolderName%"
)
So when I drag and drop multiple files into the batch file, it will take the name of the folder that holds the files that I drag and drop and make a new folder at C:\ with the same name and then move the files into the new folder at C:\
Example: The folder that holds the files that I want to move is name Shop. Then the folder Shop is located at
...\ground\bell\tower\Shop
Using the batch file will make a new folder name Shop at C:\
Example
C:\Shop
The batch files works when I drag and drop about 100 files at once. The problem is that when I drag and drop 300 files at once, it returns the Error that says "The filename or extension is too long". I am able to move the files manually so I know that it can not be cause by a filename being too long.
Then I made a new batch file thinking maybe it is a problem with the move command or my for loop. So I wrote the batch file below.
#echo off
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
MOVE "%cd%\*.*" "C:\%FolderName%"
Now the second batch file above works just about the same as the first batch file. Just that with the second batch file, I only need to drag and drop one file from the folder that I want to move and it will move all the files in the first folder to a new folder at C:\ even if the first folder had 1000 files.
My question is why does the first batch file fail if I drag and drop too many files at once. Using the second batch file work, so it can not be because of the move command or is it? Since I am moving all the files from one folder to the other, the second batch file fits my need and was wondering if there will be any problems with the second batch file or a better way of doing this.
When you Drag-n-Drop Files on you Bat File, your Bat is actually called as if you would have called it in your command prompt. So if you drag three files on your Bat i.e.
Testfile.txt
Testfile.md
Testfile.jpg
Then actually your Bat is called like this:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg
If you call more it is obviously something like:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg Testfile01.txt Testfile01.md Testfile01.jpg Testfile02.txt Testfile02.md Testfile02.jpg Testfile03.txt Testfile03.md Testfile03.jpg Testfile04.txt Testfile04.md Testfile04.jpg Testfile05.txt Testfile05.md Testfile05.jpg etc...
Ah, can you read the rest of the line? Ok, this is not as long as 1000 Files but get the point what the difference is in your scripts. Your command line buffer will not be able to capture that much input.
Actually the size of how many characters you can enter in you command prompt varies a bit from system to system, but there was something like 8k in winXP, i reckon it is still the same.
http://support.microsoft.com/kb/830473 <-- that could help more concerning max.
And since the length of your command prompt is so "short" you have to find different methods for longer file trails - as you did - you could also overcome this, if you output the files you want to copy in a text file and then use that as input for your copy bat.
dir /b >filelist.txt
so now that you have this file list, then you just read the text file line by line:
for /f "delims=" %%i in (filelist.txt) do echo D|xcopy "C:\FolderName\%%i" "c:\temp\%%i" /i /z /y
so this will actually read your file list.txt and will (for each filename in list) press D key and pipes it to the XCopy command.
So piping a filelist to your command is a much better way, where actually in your case, if you really want the whole dir and not just a selection, copying directories is faster than copying file by file.
Hope you Question is answered.
If I am not mistaken, you are trying to create a folder with the parent folder as name. So before you ask again the same question in another post, I will code what you are seeking.
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul ( mkdir "%destination%\%parent%" )
move "%%~i" "%destination%%parent%"
)
In addition, a command with many arguments is not a problem in the previous code.
For example: Write a batch file with the following code
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul (echo mkdir "%destination%\%parent%")
echo move "%%~i" "%destination%%parent%"
)
call it test-move.bat and put the following long command-line of 250 parameters in cmd:
test-move.bat TestFile001.txt TestFile002.txt TestFile003.txt TestFile004.txt TestFile005.txt TestFile006.txt TestFile007.txt TestFile008.txt TestFile009.txt TestFile010.txt TestFile011.txt TestFile012.txt TestFile013.txt TestFile014.txt TestFile015.txt TestFile016.txt TestFile017.txt TestFile018.txt TestFile019.txt TestFile020.txt TestFile021.txt TestFile022.txt TestFile023.txt TestFile024.txt TestFile025.txt TestFile026.txt TestFile027.txt TestFile028.txt TestFile029.txt TestFile030.txt TestFile031.txt TestFile032.txt TestFile033.txt TestFile034.txt TestFile035.txt TestFile036.txt TestFile037.txt TestFile038.txt TestFile039.txt TestFile040.txt TestFile041.txt TestFile042.txt TestFile043.txt TestFile044.txt TestFile045.txt TestFile046.txt TestFile047.txt TestFile048.txt TestFile049.txt TestFile050.txt TestFile051.txt TestFile052.txt TestFile053.txt TestFile054.txt TestFile055.txt TestFile056.txt TestFile057.txt TestFile058.txt TestFile059.txt TestFile060.txt TestFile061.txt TestFile062.txt TestFile063.txt TestFile064.txt TestFile065.txt TestFile066.txt TestFile067.txt TestFile068.txt TestFile069.txt TestFile070.txt TestFile071.txt TestFile072.txt TestFile073.txt TestFile074.txt TestFile075.txt TestFile076.txt TestFile077.txt TestFile078.txt TestFile079.txt TestFile080.txt TestFile081.txt TestFile082.txt TestFile083.txt TestFile084.txt TestFile085.txt TestFile086.txt TestFile087.txt TestFile088.txt TestFile089.txt TestFile090.txt TestFile091.txt TestFile092.txt TestFile093.txt TestFile094.txt TestFile095.txt TestFile096.txt TestFile097.txt TestFile098.txt TestFile099.txt TestFile100.txt TestFile101.txt TestFile102.txt TestFile103.txt TestFile104.txt TestFile105.txt TestFile106.txt TestFile107.txt TestFile108.txt TestFile109.txt TestFile110.txt TestFile111.txt TestFile112.txt TestFile113.txt TestFile114.txt TestFile115.txt TestFile116.txt TestFile117.txt TestFile118.txt TestFile119.txt TestFile120.txt TestFile121.txt TestFile122.txt TestFile123.txt TestFile124.txt TestFile125.txt TestFile126.txt TestFile127.txt TestFile128.txt TestFile129.txt TestFile130.txt TestFile131.txt TestFile132.txt TestFile133.txt TestFile134.txt TestFile135.txt TestFile136.txt TestFile137.txt TestFile138.txt TestFile139.txt TestFile140.txt TestFile141.txt TestFile142.txt TestFile143.txt TestFile144.txt TestFile145.txt TestFile146.txt TestFile147.txt TestFile148.txt TestFile149.txt TestFile150.txt TestFile151.txt TestFile152.txt TestFile153.txt TestFile154.txt TestFile155.txt TestFile156.txt TestFile157.txt TestFile158.txt TestFile159.txt TestFile160.txt TestFile161.txt TestFile162.txt TestFile163.txt TestFile164.txt TestFile165.txt TestFile166.txt TestFile167.txt TestFile168.txt TestFile169.txt TestFile170.txt TestFile171.txt TestFile172.txt TestFile173.txt TestFile174.txt TestFile175.txt TestFile176.txt TestFile177.txt TestFile178.txt TestFile179.txt TestFile180.txt TestFile181.txt TestFile182.txt TestFile183.txt TestFile184.txt TestFile185.txt TestFile186.txt TestFile187.txt TestFile188.txt TestFile189.txt TestFile190.txt TestFile191.txt TestFile192.txt TestFile193.txt TestFile194.txt TestFile195.txt TestFile196.txt TestFile197.txt TestFile198.txt TestFile199.txt TestFile200.txt TestFile201.txt TestFile202.txt TestFile203.txt TestFile204.txt TestFile205.txt TestFile206.txt TestFile207.txt TestFile208.txt TestFile209.txt TestFile210.txt TestFile211.txt TestFile212.txt TestFile213.txt TestFile214.txt TestFile215.txt TestFile216.txt TestFile217.txt TestFile218.txt TestFile219.txt TestFile220.txt TestFile221.txt TestFile222.txt TestFile223.txt TestFile224.txt TestFile225.txt TestFile226.txt TestFile227.txt TestFile228.txt TestFile229.txt TestFile230.txt TestFile231.txt TestFile232.txt TestFile233.txt TestFile234.txt TestFile235.txt TestFile236.txt TestFile237.txt TestFile238.txt TestFile239.txt TestFile240.txt TestFile241.txt TestFile242.txt TestFile243.txt TestFile244.txt TestFile245.txt TestFile246.txt TestFile247.txt TestFile248.txt TestFile249.txt TestFile250.txt
Can anyone assist me in writing a batch script that searches for file names and outputs the location of the files to a text file. Example I have a file called list.txt located in a folder, C:\LocateFiles\list.txt. Located in the list.txt file are about 25 file names that I wish to determine if they are anywhere on the C:\ drive. If it locates any of the file names identified in the file list.txt it will output the path of all files found to a single file in C:\LocatedFiles\results.txt.
A million thanks,
Johnny Mac
#ECHO OFF
FOR /F %%F IN (C:\LocateFiles\List.txt) DO DIR /s/p/b %%F.* >> C:\LocateFiles\finds.txt
Save that as LocateFiles.cmd and place it in whichever directory you wish to search, note that C:\ is very large and will take quite a while! as in, forever, seriously, i really wouldnt, your call...
the file finds.txt will have the entire path for any file that matches up to the file names listed in List.txt
Also note, this finds files of any extension, but the filename itself must match exactly to whats in List.txt
The solution below search the files in the current directory just once, so it run faster.
#echo off
dir /S /B /A-D | findstr /I /G:C:\LocateFiles\list.txt > C:\LocatedFiles\results.txt
EDIT: New method added
The method below may run even faster. It is necessary to complete a timing test.
#echo off
setlocal EnableDelayedExpansion
rem Read file names from file list and assemble a long string with this format:
rem "filename1.ext*" "filename2.ext*" ...
set "fileList="
for /F "delims=" %%a in (C:\LocateFiles\list.txt) do set fileList=!fileList! "%%a*"
rem Search the files from current directory downwards
(for /R %%a in (%fileList%) do echo %%a) > C:\LocatedFiles\results.txt