Renaming issue within command prompt - cmd

i'm trying to rename a file called Pawn.Stars.S01E02.Hello
i'm using
cmd ren "Pawn*Stars*" "Pawn Star$.*"
which does rename the file but renames it to Pawn Star$.Stars.S01E02.Hello
i have also tried
ren Pawn.Stars*.txt PawnStar$*.txt
which gives the same result.
The Name i'm trying to get is Pawn Star$.S01E02.Hello
Where am i going wrong or what needs to change?

You haven't stated what name you are trying to achieve, so I cannot give you a command that works.
But I can tell you that it is not possible to remove or change the dot between Pawn and Star$ using a simple REN command with wildcards. See How does the Windows RENAME command interpret wildcards? for an explanation.
One option would be to write a batch script to do the rename. There are also 3rd party utilities that provide more sophisticated file renaming capabilities.
Update based on comment from OP:
The following will do the rename from the command line:
for /f "delims=" %A in ('dir /b /a-d pawn.stars.*') do #for /f "delims=. tokens=2*" %B in ("%A") do #ren "%A" "Pawn Star$.%C"
Double up the percents if you use the command in a batch script.

Related

Rename dynamic file using batch script

Hi I have multiple file which are having tie stamp (random).
PMPRO_Outbound_US05_20170927_114630.csv
PMPRO_Outbound_US05_20170928_115430.csv
etc.
I want to rename these files using batch script, could you please help.
PMPRO_Outbound_US05_20170927_114630.csv
to
PMPRO_Outbound_US05_20170927_114630.csv_1
Please help.
using a for loop:
for /F %%i in ('dir /b *.csv') do echo mv %%i %%i_1
Once you are comfortable that it is doing what you intended to do, remove echo to perform the actual rename.

Want to rename a file through windows batch file scripting

Here is my file MY_APP_CON_123_yyyymmdd.hhmmss.txt where "yyyymmdd.hhmmss" is variable data. I want to rename it to MY_APP_CON_0123_yyyymmdd.hhmmss.txt (Zero infront of 123). When I use ren command as shown below, the first y disappears from the file name.
REN MY_APP_CON_123_* MY_APP_CON_0123_*
Any solutions ?
Not answer, but explaining why your rename isn't working:
cmd.exe/command.com's wildcard renaming is position based, e.g.
filename: abc123.txt
command: ren abc*.txt abd1*.txt
will do:
abc123.txt - original name
||||**.txt
abd1**.txt - rename pattern
----------
abcd23.txt - new filename
https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards
Wildcard renaming where you want to change parts of the filename and the new part has a different length from the original part gets very hairy.
Here is a pure batch solution.
#echo off
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&call ren "%%name%%" "%%name:*MY_APP_CON_=MY_APP_CON_0%%"
Or, with delayed expansion:
#echo off
setlocal enableDelayedExpansion
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&ren "!name!" "!name:*MY_APP_CON_=MY_APP_CON_0!"
But I would use my JREN.BAT regular expression renaming utility - a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
call jren "^MY_APP_CON_123_" "MY_APP_CON_0123_" /i /fm *.txt
for %%a in (MY_APP_CON_123_**) do set "name=%%a" & ren %name% %name:123=0123%
This worked for me

How to search a hard drive for a directory name using Windows batch file and strip only the directory name

I am building a .bat file that requires me to find a directory on a hard drive.
I have used the dir command to output the following:
G:>dir woff.exe /s /b
G:\OBDSoftware\WOFF\OBDWW1 Over Flanders Fields\WOFF.exe
I want to store this info in a variable and then possibly use findstr to strip out the "WOFF.exe" portion of the above string and only save the first part "G:\OBDSoftware\WOFF\PBDWW1 Over Flanders Fields\" in another variable for further use. I am having difficulty understanding how to do this.
I would appreciate some help as I am rather new to batch files.
Thanks in advance for any help
This should get you started:
EDIT
I added "delims=" to the FOR command to handle spaces in the paths.
FOR /F "delims=" %%I IN ('dir woff.exe /s /b') DO SET myvar=%%~dI%%~pI
SET myvar=%myvar:~0,-1%
Then you can use %myvar% to access the stored file path.

Get file name and append to beginning of line

I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?
No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.
In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity

How do I find a directory in and store it into a variable, using a Windows batch file?

I need to find the location of a specific directory, and then store that directory path into a variable within a Windows batch script.
I also want the command to return when it finds a match (to avoid searching the entire hard drive once the directory has already been found).
So far I've tried this on the command line:
dir c:\ /s /b /ad | find "DirectoryName"
The problem with this is that it searches the entire drive, even after a match is found. Plus, I still can't figure out how to store the result in a variable within a batch file. There should only be a single result.
Basically I need the equivilent of somehting like this on Linux/bash:
export DIRPATH=`find / -name "DirectoryName" -print -quit`
Thanks for looking!
In batch you need FOR /F to get the output of a command.
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "DirectoryName"`) DO (
set "DIRPATH=%%p"
)
echo %DIRPATH%
As there are quotes in the find command you need the usebackq-option.
And it's necessary to escape the pipe character one time, as it should pipe the dir command, not the for command

Resources