Renaming files overwrites part of the filename - windows

I ahve a directory full of files, and I want to rename each of them to have TA_ in front of the original file name. file1.txt should be renamed to TA_file1.txt. What I am getting is TA_e1.txt instead.
ren "c:*.txt" "TA_*.txt" is the command I am trying to use.
The file names are all of various lengths, and no matter what I try, it always overwrites the first 3 characters of my file name....

A simple one liner would be:
for %i IN (*.txt) DO ren "%i" "TA_%i"
This loops over all files (*.txt) and passes their name in the %i variable to the ren command. ren can then use the %i content to expand it with your desired prefix.
The command will only work for files in the current directory. For more complex things you should write a batch file. Come back if you need help with that.

Related

Windows Batch script to rename files with it's folder name within a for loop

I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"

Convert list of paths to list of filenames

Background
I find myself often copying file paths to the clipboard, which is somewhat cumbersome to do from Windows Explorer.
So I wrote a little .bat file to put into the %APPDATA%\Microsoft\Windows\SendTo\ folder utilising the CLIP executable to copy a list of the selected file paths to the clipboard. This file consists only of a single line:
echo|set /p= "%*" | clip.exe
Which works quite nicely, I can select one or more filenames in Explorer, right-click on them and "Send To" the .bat file, which copies them to the clipboard. Each file path is complete and separated from the others by a space character.
Question
Sometimes, I don't want to copy a list of the full file paths, but would prefer to have a list of just the filenames with their extensions. I know how to do that conversion for single file paths, using the %~nx syntax as described here or here.
I tried different combinations of these but can't seem to find a workable solution for my list of paths. The following code echos the filenames correctly:
for %%F in (%*) do echo %%~nxF
...but how do I combine them to pass through to CLIP? Do I have to do string concatenation? Maybe in a subroutine to be called, or is there a more elegant solution?
The following will put each file name on a separate line within the clipboard:
#(for %%F in (%*) do #echo %%~nxF)|clip
If you prefer, the following will put a space delimited list of file names on a single line, with quotes around each file name.
#(for %%F in (%*) do #<nul set /p =""%%~nxF" ")|clip
Couldn't you just:
echo|set /p= "%~nx*" | clip.exe

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Windows batch file - Concatenate all files in subdirectories

Need to concatenate all of the javascript files in a directory and all of its sub-directories into one file.
Right now I have a very simple command in a batch file that concatenates all of the matching files in one directory into one file:
copy C:\javascripts\*.js concatenated.js
However, this only works for the one directory and not any of its sub-directories. How can I do this same thing and also include all of the matching files in the sub-directories?
Thanks so much!
From the command line you can use
for /r "c:\javascripts" %F in (*.js) do #type "%F" >>concatenated.js
You might want want to first delete any existing concatenated.js before you run the above command.
From a batch file the percents need to be doubled
#echo off
del concatenated.js
for /r "c:\javascripts" %%F in (*.js) do type "%%F" >>concatenated.js
EDIT
It is a bit more efficient to put parentheses around the entire statement and use a single overwrite redirection instead of append redirection with each iteration. It also eliminates the need to delete the file in the beginning.
>concat.js (for /r "c:\javascripts" %F in (*.js) do #type "%F")
or from batch
#echo off
>concat.js (for /r "c:\javascripts" %%F in (*.js) do type "%%F")
I'm not aware of an approach to do that from a batch file, but you could use a tool like minify
http://code.google.com/p/minify/
to both combine JavaScript files and also minify them (e.g. by removing comments and unnecessary white space).
There are quite a few similar tools for a variety of programming environments.

Copying all file contents into new files - Batch

What I need to do is (and I have some idea, just can't get this solidified):
Read all files in a directory (for loop)
In loop, I need to copy the current file in the loop's contents into a new file (x.txt into x2.txt) in the same directory. This can be done with a type >> command.
I'm more confused about the looping part of it. How do I do that?
Edit:
This is my current script:
FOR %%i IN (*)
Do type %%i >> %%i2
This will copy each .txt file to a backup file with extension .txt2:
for %i in (*.txt) do type "%i" > "%i2"
Note that in a batch file you need to double the %:
for %%i in (*.txt) do type "%%i" > "%%i2"
It's not clear from your question if you want all found files dumped into the same resulting text file or if you want one-to-one copies of each.
If you just want one results file, you could also use a variant of the copy command, i.e.:
copy *.txt output.dat

Resources