Right now I can use this with some success but I cant get it to work recursively? Any help would be awesome! I have been googling all day to find a solution and I havent found anything else that works.
for /f "Tokens=*" %%f in ('dir /l/b/a-d') do (rename "%%f" "%%f")
it has been suggested to do this but I've had no success.
for /f "Tokens=*" %%f in ('dir /l/b/a-d/s') do (rename "%%f" "%%f")
The rename command only takes a file name as the second parameter.
C:\>rename /?
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
Here are the corrected rename parameters
for /f "tokens=*" %%F in ('dir /l/b/a-d/s') do rename "%%~fF" "%%~nxF"
Open a Command Prompt.
Go to the folder with the cd command (eg.: cd "path of your folder").
Open a powershell by typing: powershell.
Then input this:
get-childitem -recurse | Where {-Not $_.PSIsContainer} | Rename-Item -NewName {$_.FullName.ToLower()}
Related
I have files such as:
How can I replace example to test-file in each of them?
I want solutions for:
Batch file
PowerShell
Bash
This is a batch-file solution:
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%A IN ('dir /B /A-D "example*"') do (
set "fname=%%~nA"
rename "%%~fA" "!fname:example=test-file!%%~xA"
)
which should work. If you don't want to hardcode the word to replace, use:
#echo off
setlocal EnableDelayedExpansion
set "word=test-file"
for /F "delims=" %%A IN ('dir /B /A-D "example*"') do (
set "fname=%%~nA"
rename "%%~fA" "!fname:example=%word%!%%~xA"
)
For cmd one-line use:
for /F "delims=" %A IN ('dir /b /a-d "example*"') do #set "fname=%~nA"&call rename "%~fA" "%fname:example=test-file%%~xA"
Here's a batch-file solution:
#For %%A In (example*)Do #Set "_=%%A"&Call Ren "%%A" "test-file%%_:*example=%%"
As one has not yet been submitted, here's a powershell answer:
Get-ChildItem -Filter 'example*' | Rename-Item -NewName {$_.Name -Replace '^example','test-file'}
You could of course utilise PowerShell from a batch-file:
#Powershell -NoP "GCI -Filt 'example*'|RnI -N {$_.Name -Replace '^example','test-file'}"
For a bash solution, you're going to need to be more specific, because different OS'es/distributions etc. use different methods, some may use rename from util-Linux others may use the Perl rename utility. Others may not have rename and need to utilise a for loop with the mv command.
for f in example*; do mv "$f" "${f/example/test-file}"
[Edit /]
To run a command from the Command Prompt despite your lack of a cmd tag:
For %A In (example*)Do #Set "_=%A"&Call Ren "%A" "test-file%_:*example=%"
In bash you can use rename command like this:
rename example test-file example*
Here first argument is required expression to be changed, second argument is replacement and last argument is file name
I am trying to add file extensions to a large number of files located in a series of folders and subfolders in Windows. For some reason, these files do not have a file extension on them and I need them to have the extension .ddd so I can convert them to PDFs using a separate program.
cd R:\PRODUCTION\92
ren *. *.ddd
Note that this command does indeed work but only on folders that actually contain the files I need, and no subfolders. What could I add or change to hit all files in all subfolders? Thanks in advance.
Please try the following command. If it looks like it will do the correct rename, remove the ECHO from the REN command line.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%~f" "%~nf.ddd")
In a .bat file script, double the percent character on the variable.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %%f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%%~f" "%%~nf.ddd")
My current folder structure goes as follows:
E:\Videos\Movies\Random Folder Name\Subs\Random File Name.srt
I would like to move my .srt files up one level so it reads:
E:\Videos\Movies\Random Folder Name\Random File Name.srt
I would prefer this to be a .bat file, but am willing to use PowerShell.
~EDIT~
I found something online that partially works and edited it to my needs:
#echo off
set thisdir=%cd%
for /f "delims=" %%A in ('dir /b /ad') do (
cd /d "%%~dpnA"
for /f "delims=" %%B in ('dir /b /ad') do (
echo Level 2 Directory: %%~dpnB
cd /d "%%~dpnB"
for /f "delims=" %%C in ('dir /b /ad') do (
echo Level 3 Directory: %%~dpnC
cd /d "%%~dpnC"
move *.srt ..\
cd..
rd "%%~dpnC"
)
)
)
This works, but only for the first folder, I can't seem to make Level 2 recursive as that is the level with random movie names. I tried replace for /f with for /r, but it was a no go.
Here's a one-liner:
forfiles /m *.srt /s /c "cmd /c move #file .."
Full code (you can run this from any drive now):
#echo off
cd /d E:\Videos\Movies\
for /r %%i in (*.srt) do move "%%~dpnxi" "%%~dpi.."
pause
This looks for all files with type .srt and moves them to the folder it was found in -1 directory (%%~dpi is the directory it was found in, adding .. to a path removes the last directory, so C:\Users\.. would put you at C:\).
PS: This time I have tested this, and it works.
Although the answers already given work, I still wanted to try and figure out how to perfect the code to my exact needs. Couldn't accomplish this with CMD, so I looked into powershell (which was easier for me to grasp for some reason) and coded this:
$sourcefolder = "F:\Videos\Movies\*\Subs\"
$files = Get-ChildItem -Recurse $sourcefolder | where {$_.PSIScontainer -eq $false}
foreach ($file in $files)
{
$destinationFolder = Split-Path -Parent $file.Directory.FullName
move-item $file.FullName $destinationFolder
}
It doesn't specify .srt files, but they are the only extension located in that folder. Thank you for the help guys!
Following Windows batch command converts all tif images in the folder C:\RootFolder\Folder1.
for %%i in (C:\RootFolder\Folder1\*.tif) do "Tiff2Pdf.exe" -o C:\RootFolder\Folder1\%%~ni.pdf %%i
How can I do it for all the folders available in RootFolder?
RootFolder
-Folder1
-Folder2
-Folder3
.
.
Thanks for your time
There's another way - just to add it:
#echo off
for /r "c:\rootfolder\folder1" %%a in (*.tif) do "Tiff2Pdf.exe" -o "%%~dpna.pdf" "%%a"
I also changed the loop variable to a because i is close to l and I and 1 in many fonts.
FOR /F "delims=" %%i IN ('dir /b /s C:\RootFolder\Folder1\*.tif') DO "Tiff2Pdf.exe" -o "%%~dpi%%~ni.pdf" "%%i"
Use dir /s /b to do a full recursive enumeration
Use FOR /F "delims=" to parse the results and handle paths with spaces.
Use the %%~dpi%% to get the directory of each file.
Use %%~ni to get the file's name with out an extension.
I just want to know how can I get all the names of the folders in a current directory. For example in my current directory I have three folders:
stackoverflow
reddit
codinghorror
Then when I execute my batch script all the three folders will print in the screen.
How can I achieve this?
Using batch files:
for /d %%d in (*.*) do echo %%d
If you want to test that on the command line, use only one % sign in both cases.
On Windows, you can use:
dir /ad /b
/ad will get you the directories only
/b will present it in 'bare' format
EDIT (reply to comment):
If you want to iterate over these directories and do something with them, use a for command:
for /F "delims=" %%a in ('dir /ad /b') do (
echo %%a
)
note the double % - this is for use in a batch, if you use for on the command line, use a single %.
added the resetting of default space delims in response to #Helen's comment
With PowerShell:
gci | ? { $_.PSIsContainer }
Old Answer:
With PowerShell:
gci | ? {$_.Length -eq $null } | % { $_.Name }
You can use the result as an array in a script, and then foreach trough it, or whatever you want to do...
For getting all the subfolders of a specific directory and display it in CMD :
#echo off
dir C:\input /s /b /o:n /a:d
Pause&Exit
For getting all the subfolders of a specific directory and save it in a text file :
dir C:\your_directory /s /b /o:n /a:d > output.txt