shell script to rename a file name based on another file name - cmd

I have 2 files in my folder. One is "TEST ONE.txt", the other is "LEARN NEW.vcd".
I need to create a batch script to rename the *.txt using the *.vcd.
So the Batch script should rename the "TEST ONE.txt file" to "LEARN NEW.txt".
I have only one file with txt and one file with vcd extension in my folder C:\DEV folder.
I tried using the following script but it does not pick up the full name of the vcd extension and drops everything after the spaces - so I just get LEARN.txt :(
#echo ON
CD "C:\DEV folder"
setlocal enabledelayedexpansion
for /F %%A in ('dir /b *.vcd') do (
set xname= %%~nA
ren *.txt !xname!.txt
)
Thanks - db

It appears that the default in FOR /F may be to only take one (1) token. You can force it to take all using tokens=*. Quoting is important.
#echo ON
CD "sub"
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in (`dir /b *.vcd`) do (
set xname=%%~nA
ren "*.txt" "!xname!.txt"
)

Just in case someone stumbles across this the way I did, there is a more elegant solution that I was forced to devise because I wanted it to go through multiple sub-directories, rather than run it one directory at a time. It is:
forfiles /S /M *.vcd /C "cmd /c rename *.txt #fname.txt"
Use your file extensions of choice.

Related

How to rename files based on CSV file with FOR /F loop?

I'm trying to automate renaming of files based on a CSV such as the one shown below:
Name,FullName
John,JohnDoe
Jane,JaneDoe
Joe,JoeDoe
Let's say I have 3 text files within the same folder of my .bat called John.txt, Jane.txt, Joe.txt and I want to rename John.txt to JohnDoe.txt, etc.
I am getting "The system cannot find the file specified" no matter how much I alter the filepath in my rename. Here is a basic rundown of what I have. What am I doing wrong here or what other way should I approach this? I appreciate all feedback.
#echo off
setlocal enabledelayedexpansion
set csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (!csvpath!) do (
set person=%%g
set name=%%h
echo My name is !person! and my full name is !name!
rename !person!.txt !name!.txt
)
pause
This is how I would do it:
#echo off
set "csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv"
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (`findstr /v /c:":-:-:" "%csvpath%"`) do (
echo My name is "%%g" and my full name is "%%h"
rename "%~dp0\%%g.txt" "%%h.txt"
)
pause
This code is a bit cleaner and more robust, in that file paths and names can have special characters (like &) without breaking the script.
findstr /v /c:"SEARCHSTRING" "FILEPATH" tells findstr to print every line within FILEPATH excluding (/v) lines with SEARCHSTRING. This doesn't really change much from what you had previously, however it is a bit more robust.
In the rename command, I set it to %~dp0 and then the file name, %~dp0 is the path to where your .bat script is.

I need a batch file to generate a list with *only* file names

I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.

Windows batch - match folders with a dot (.) in the folder name

I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa

dos command to delete a folder except the named sub folders

Assume I have a directory in your computer under C:/dir1
And inside "dir1" we have more directories dir11, dir12, dir13,
And in each of the above directories we have more like below
dir11- dir111, dir112 dir113
dir1-dir121, dir122, dir123
dir13 - dir132,dir132,dir133
Now I need to find a command or a small script, which can delete everything under dir1 except couple of directories say dir122 and dir132.
Can you find something using DOS commands ?
You can use dir filespec /b >tmp.bat to list the file names in a text file called tmp.bat. Then edit that file to add del before each file name you want to delete. You can remove the file names you want to keep from the batch file and then do a "change all" to add del to each line. When it is edited properly, execute the batch file.
Alternatively, you could write a quick program under vb.net to do it.
Using the given examples: this should echo all the rd commands for the other folders.
#echo off
for /f "delims=" %%a in (' dir "c:\dir1" /b /s /ad ^| findstr /v /i "dir122 dir132" ') do echo rd /s /q "%%a"

How do I iterate over a set of folders in Windows Shell?

I'm currently trying to write a .cmd Windows Shell script that would iterate over a set of folders. However even the following simplest script:
echo "%ROOT%"
for %%f in ("%ROOT%\Binaries\" ) do (
echo "%%f"
if not exist "%%f\Subfolder"
md "%%f\Subfolder"
)
outputs:
CurrentDir>echo "<ActualPathToRoot>"
"<ActualPathToRoot>"
%f\Subfolder was unexpected at this time
CurrentDir>if exists "%f\Subfolder"
What am I doing wrong? How do I alter that script so that it iterates over that one folder and once it see there's no subfolder named "Subfolder" it creates that subfolder? Also is there a good tutorial on writing such scripts?
For (sub)folder-iteration you need to use a different for parameter.
So if you want to list all directories of C: you should do this:
for /d %%A in (C:\*) do echo %%A
Note the parameter /d which indicates a directory. To go into subdirectories you need to do a recursive for with /r
for /r C:\Windows %%A in (*.jpg) do echo %%A
This would iterate through all Windows subdirectories looking for JPGs. Low behold you should be able to do /d /r and this reference suggests you can - I simply can't, but maybe you are able to do this?
A workaround I quickly jotted down is to just do a dir of all directories in a for loop:
for /f "delims=" %%A in ('dir /ad/s/b') do echo %%A
Note that dir is used in conjunction with /ad/s/b which performs a recursive listing of directories, printing the names of the directories found.
With these tools in your hand you should be able to do your if-subfolder construct. Note that you might need
This works for me:
echo %ROOT%
for /D %%f in (%ROOT%\Binaries\*) do echo %%f && if not exist %%f\Subfolder md %%f\Subfolder

Resources