Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to write a windows batch script that lists all extensions in a directory, recursively.
The desired behaviour is this:
You start the bat file in directory "a" which contains the following files: b.txt, c.png, d.txt, e.jpeg, f.jpg.
The script should output the following:
txt
png
jpeg
jpg
I would like the each extension to be in a new line, alphabetic ordering is not necessary.
The method you linked to is probably overkill for what you want.
Do a for /r loop to get all files recursively and set a variable named the same as the extension (the value isn't important, we need only the variable to be defined)
Then do a for /f loop to list the variables (with set . - all of them start with a dot conveniently because the %%~x modifier gets the extension with the dot (.txt)). Then just sort them alphabetically, if needed:
#echo off & setlocal
for /r %%a in (*) do set "%%~xa=X" 2>nul
for /f "delims==" %%a in ('set .') do echo %%a
(edit: removed the sort, because set . already sorts alphabetically - thank you #LotPings for the hint)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have directory which contain no. of file like this
abac_273#jj.txt ,
hhh.78448#kkpp.txt ,
dgfhf#ytyt#llltyui.txt
I need to write batch script where I need to rename those file like this
jj.txt , kkpp.txt, llltyui.txt
In simple words, I need to find out # from end and return string after # as output.
Can you please help me out to solve this
for /f "tokens=1*delims=#" %%a in ('dir /b /a-d "*#*"') do echo(ren "%%a#%%b" "%%b"
should provide what you want - reduce each %% to % if you execute this from the prompt instead of as a batch line.
If it works correctly for you, change the echo(ren to ren to actually do the change - this code will simply report the proposed change to the screen.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In Windows 7, I have a folder which only contains subfolders and no files. Within the subfolders, I have a bunch of .dcm files (medical images). The files within each subfolder have the same name, and I want them to all have different names, more specifically, I want the names to be order 1.dcm, 2.dcm, 3.dcm, etc.
Current situation:
Folders A, B, and C, and each folder contains File1.dcm, File2.dec, File3.dcm
Would like to have this:
Folder A contains files 1.dcm, 2.dcm, 3.dcm
Folder B contains files 4.dcm, 5.dcm, 6.dcm
Folder C contains files 7.dcm, 8.dcm, 9.dcm
Is there a way to write a batch file that goes into each subfolder in order and renames the files in numerical order? I am very much the novice when it comes to writing script for Windows and have no idea how to go about doing this. I found code that changes file extensions within subfolders, but nothing that does what I am looking for.
Any help is greatly appreciated!
#Echo off
For /f "tokens=1* delims=:" %%A in (
'Dir /B/S/ON/A-D "X:\path\to\folder\*.dcm" ^|findstr /N /V "^$"'
) Do Echo Ren "%%~fB" "%%A%%~xB"
If the output seems ok remove the echo in the last line.
If you have numbers with different length in one folder be aware that sorting by name will have 10 before 2.
For better understanding some explanations:
Dir /B/S/ON/A-D "X:\path\to\folder*.dcm"
/Bare format
/Subdtree recurse
/Order by Name
/Attribute -(not) Directory
Findstr /N /V "^$"
/Number each output line delimit with a colon.
/inVerse the match
"^$" a Regular Expression meaning an empty line
^ matches the begin of a line
$ matches the end of a line
For /f "tokens=1* delims=:" %%A in ('command') do
Parses output of 'comand' into tokens 1=%%A and *=rest into %%B
the delimiter : is only used for token 1,
token 2 takes the rest of the line (important to preserve the drive colon)
The vars %%A and %%B are passed to the do (part)
The for vars can be modfied with ~ modifiers see For /?
%%~fB ~f meaning fullname including dirve and path,
%%A%%~xB ~x is the extension from %%~xB including the dot
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to remove certain characters from multiple files which share the same extension. An example would be: filename = 01songname.mp3 to songname.mp3, but doing that command to all the mp3 files in the directory, not renaming to one name but removing the numbering.
Based on your existing examples this may work for you.
#echo off
FOR %%M IN (*.mp3) DO (
FOR /F "tokens=* delims=0123456789" %%G IN ("%%~M") DO ECHO RENAME "%%~M" "%%~G"
)
pause
Remove the word ECHO before the word RENAME if you feel the output on the screen looks correct.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How will I accomplish the following in a FOR statement?
I have files in a windows PC directory that I want to identify by all or part of a datetime stamp that is part of the files name, ie-
CLIENT CODE / ID DATETIME STAMP
0000090000010009.CLIENTNAME.20121212140022.txt
0001090000010009.CLIENTNAME.20130916110025.txt
0001090000010009.CLIENTNAME.20130908150022.txt
I do not have to open/read the file. I just need to identify files in a particular date time range to either MOVE, DELETE or COPY them.
for /f "tokens=1-4delims=." %%a in ('dir /b /a-d "*.clientname.*.*"') do (
set "dts=%%c"
setlocal enabledelayedexpansion
set "yyyy=!dts:~0,4!"
set "mm=!dts:~4,2!"
set "dd=!dts:~6,2!"
set "hh=!dts:~8,2!"
set "min=!dts:~10,2!"
set "sec=!dts:~12,2!"
echo File: %%a.%%b.%%c.%%d date/time: !dd!/!mm!/!yyyy! -- !hh!:!min!:!sec!
endlocal
)
You can use the dir command with wildcard. So say you want the file 0000090000010009.CLIENTNAME.20121212140022.txt you can do:
dir *20121212140022*
If you want to put it in a loop then for /F will do the trick:
for /F %x in ('dir *20121212140022* /b /a-d ') do del %x
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I want to run a FINDSTR dos command in a way beyond what it is easily found in "findstr /?". How would I run findstr so that it only searches ascii files. (I am not sure if that is possible. My gut feeling is that it is not possible) Additionally, how would I run this command line so that it would exclude some file times. For example, what if I wanted to exclude .psd files
What is wrong with the /P option that is described in the help?
/P Skip files with non-printable characters.
It worked for me.
To take further control of which files are searched, simply iterate the files with a for loop and add whatever logic you need. To skip .psd files and also skip binary files:
for /f "eol=: delims=" %%F in ('dir /a-d /b^|findstr /live ".psd"') do findstr /p "search" "%%F"
Use a single percent instead of double percents if run from the command line.