Convert list of paths to list of filenames - windows

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

Related

writing a batch file for loop to rename and move files

I've never used Windows cmd scripting before; I'm trying to write a batch script, What I need to do:
I have a lot of folders, named numerically. Each one contains a file. All the files have the same name.
e.g.
folder1\file folder2\file
I want to rename and move the files, so they are named numerically and in the one folder
e.g.
newfolder\file1 newfolder\file2
My script for two test folders is:
FOR /L %%A IN (1,1,2) DO
(
move "folder%%A\file.txt" "newfolder\file%%A.txt"
)
I suspect this is all wrong. I get "the syntax of the command is incorrect".
Just move the opening parenthesis on the first line:
FOR /L %%A IN (1,1,2) DO (
move "folder%%A\file.txt" "newfolder\file%%A.txt"
)
Newlines aren't as invisible to the batch interpreter as in most other languages, meaning you have to explicitly tell it to look on the following lines.

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.

Copying files from a list

I have a list of 4000 .tif images that I need copied from a folder containing 20,000+ images.
I am trying to use command prompt to do so by using code found from googling my problem.
the code is as follows:
for /f %a in H:\list.txt do copy %a H:\new
"H:\list.txt" is my list file
"H:\new" is where i want my files to be copied to
I am running this command in the file folder that contains the .tif files H:\Doc2 and I keep getting:
H:\list.txt was unexpected at this time.
What is causing the issue? Is my list not correctly set up?
It looks like this:
ABBA.TIF
ABBD.TIF
ABBQ.TIF
Do i need commas or colons after the file names?
H:\list.txt is a literal, you want to loop over each of the lines of the file. Check this out:
How do you loop through each line in a text file using a windows batch file?
for /F "tokens=*" %%A in (myfile.txt) do [process] %%A
Suggestion: tag this with "windows" as well.
Amir diagnosed your immediate problem - your code is missing the parentheses around the IN clause.
But you have another potential problem. Your list may include file names and or paths that contain spaces or other special characters. If so, then the name must be quoted.
The values in the list may already by quoted, or they may not. You can use the ~ modifier to strip any existing enclosing quotes that may or may not be there, and then explicitly add your own.
for /f %a in (H:\list.txt) do copy "%~a" H:\new
If you want to include the line in a batch file, then each % must be doubled as %%.

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.

Renaming files overwrites part of the filename

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.

Resources