- Windows batch file — For All files in given directory and subdirectories — change file extension to lowercase
- 2 Answers 2
- copying all contents of folder to another folder using batch file?
- 12 Answers 12
- Iterate all files in a directory using a ‘for’ loop
- 17 Answers 17
- Rename all files in a directory with a Windows batch script
- 2 Answers 2
- Explanation
- Batch file to execute all files in a folder
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged batch-file dos or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
Windows batch file — For All files in given directory and subdirectories — change file extension to lowercase
My aim: Use Windows batch script to ensure that all files within given directory and subdirectories have a file extension that is lowercase
I have managed to get this far (not very far I admit !)..
this successfully prints out all files with an extension (inc full path) but how do I check the file extension and ensure it is lowercase (I do not want to make the filename lowercase by the way .. just the extension).
Thanks in advance !
2 Answers 2
There are 2 (possibly 3) techniques you need to accomplish your task.
1) You need to parse out the file name and file extension — that is trivial as described in the FOR HELP. (type HELP FOR from the command line)
xG = file extension
2) You need to store each file extension in a variable and then use code like the following to convert it to lowercase
You will need to enable delayed expansion with setlocal enableDelayedExpansion at the top of the script.
The above works because the search portion of search and replace is case insensitive.
3) In the unlikely situation that a file extension might containn a ! , you need to toggle delayed expansion on and off within the loop. But I seriously doubt you will run into that situation.
Here is a finished, functioning script that disregards point 3.
Bali C had an interesting idea in his answer, but it did not quite work. I managed to make it work by creating a temporary file with a name consisting only of the file extension in a temporary folder. But it is slower than the above solution.
copying all contents of folder to another folder using batch file?
I have a folder in C:\Folder1
I want to copy all the contents of Folder1 to another location, D:\Folder2
How do I do this using a batch file?
12 Answers 12
xcopy.exe is the solution here. It’s built into Windows.
If you have robocopy,
if you want remove the message that tells if the destination is a file or folder you just add a slash:
xcopy /s c:\Folder1 d:\Folder2\
I see a lot of answers suggesting the use of xcopy. But this is unnecessary. As the question clearly mentions that the author wants THE CONTENT IN THE FOLDER not the folder itself to be copied in this case we can -:
Thats all xcopy can be used for if any subdirectory exists in C:\Folder1
RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.
xcopy /i /e «C:\temp\folder 1» «C:\temp\folder 2»
Here is the documentation from Microsoft:
On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe
That’s why I use simply «copy»: copy /y . \Folder1\File.txt . \Folder2\
This is how it is done! Simple, right?
Here’s a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:
/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name
Note: it can be necessary to run the command as administrator, because of the argument /COPYALL . If you can’t: just get rid of it.
FYI. if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a «safe» location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn’t prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.
And, that’s it folks!
Add to your scheduled tasks and never look back.
I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point, Hope this would help,
Iterate all files in a directory using a ‘for’ loop
How can I iterate over each file in a directory using a for loop?
And how could I tell if a certain entry is a directory or if it’s just a file?
17 Answers 17
This lists all the files (and only the files) in the current directory:
Also if you run that command in a batch file you need to double the % signs.
nxi ) . This thread can be really useful too: stackoverflow.com/questions/112055/…. – Sk8erPeter Dec 21 ’11 at 21:25
- . files in current dir: for %f in (.\*) do @echo %f
- . subdirs in current dir: for /D %s in (.\*) do @echo %s
- . files in current and all subdirs: for /R %f in (.\*) do @echo %f
- . subdirs in current and all subdirs: for /R /D %s in (.\*) do @echo %s
Unfortunately I did not find any way to iterate over files and subdirs at the same time.
Just use cygwin with its bash for much more functionality.
Apart from this: Did you notice, that the buildin help of MS Windows is a great resource for descriptions of cmd’s command line syntax?
To iterate over each file a for loop will work:
for %%f in (directory\path\*) do ( something_here )
In my case I also wanted the file content, name, etc.
This lead to a few issues and I thought my use case might help. Here is a loop that reads info from each ‘.txt’ file in a directory and allows you do do something with it (setx for instance).
There is a subtle difference between running FOR from the command line and from a batch file. In a batch file, you need to put two % characters in front of each variable reference.
From a command line:
From a batch file:
This for-loop will list all files in a directory.
«delims=» is useful to show long filenames with spaces in it.
‘/b» show only names, not size dates etc..
Some things to know about dir’s /a argument.
- Any use of «/a» would list everything, including hidden and system attributes.
- «/ad» would only show subdirectories, including hidden and system ones.
- «/a-d» argument eliminates content with ‘D’irectory attribute.
- «/a-d-h-s» will show everything, but entries with ‘D’irectory, ‘H’idden ‘S’ystem attribute.
If you use this on the commandline, remove a «%».
Rename all files in a directory with a Windows batch script
How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.
2 Answers 2
A FOR statement to loop through the names (type FOR /? for help), and string search and replace (type SET /? for help).
UPDATE — 2012-11-07
I’ve investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?
It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.
The number of characters after the _ does not matter. The rename would still work properly if 120×90 became x or xxxxxxxxxx . The important aspect of this problem is that the entire text between the last _ and the . is replaced.
As of Windows 7 you can do this in one line of PowerShell.
Explanation
powershell -C «. » launches a PowerShell session to run the quoted command. It returns to the outer shell when the command completes. -C is short for -Command .
gci returns all the files in the current directory. It is an alias for Get-ChildItem .
| % <. >makes a pipeline to process each file. % is an alias for Foreach-Object .
$_.Name is the name of the current file in the pipeline.
($_.Name -replace ‘120×90′, ’67×100’) uses the -replace operator to create the new file name. Each occurrence of the first substring is replaced with the second substring.
rni changes the name of each file. The first parameter (called -Path ) identifies the file. The second parameter (called -NewName ) specifies the new name. rni is an alias for Rename-Item.
Batch file to execute all files in a folder
I need a batch files the launches all files in a given folder (in this case it is c:\macros\day).
I’ve tried the following, but it doesn’t seem to do anything.
6 Answers 6
This works from my command line:
You used the incorrect variant of for . simply do (pun intended) for %%i in (c:\macros\Day\*) do %%i
Edit: If you need to run a command on all files: for %%i in (c:\macros\Day\*) do command %%i
You should use dir /b to list all files, so it becomes
Also, make sure that you make variables inside batch files %%i rather than %i otherwise you get an error in the form «i was unexpected at this time.»
An alternative is posted above — leave off the trailing «/Z /U» if you wish.
This is how I could run all powershell files in same directory as the batch file
Not the answer you’re looking for? Browse other questions tagged batch-file dos or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.