For example, I have the folder d:\temp\ and four word document files in it (.doc)
I know that
dir /b "d:\temp"
will give me
File1.doc
File2.doc
File3.doc
File4.doc
But how can I do it so that there are only file names without extensions?
like
File1
File2
File3
File4
for %a in ("d:\temp\*") do #echo %~na
or for batch file:
for %%a in ("d:\temp\*") do #echo %%~na
to display also directories you can use:
for /f "delims=" %%a in (' dir /f "d:\temp\*"') do #echo %%~na
Another version with slight differences from the ones above:
for /f %x in ('dir /b /on *.doc') do #echo %~nx
The following code, when run via a batch file that uses #echo off, should work:
for /f "skip=7 tokens=5 delims=. " %%g in ('dir d:\temp') do echo %%g
Will print all file names without spaces in them. Tested and does so for my F:\ thumb drive.
Edit: for /f "tokens=1 delims=." %%g in ('dir /b d:\temp') do echo %%g
Will print out files and directories in a given path with spaces and is simpler anyway.
Related
I'm trying to read off a .txt file that contains a list of names of .jpgs I want from another directory, and then copy those files to the current directory. The name of the text file can't be hardcoded, as I intend to use the same batch file for hundreds of folders, each with their own unique .txt list.
I've tried this:
FOR /F %%a in ("*.txt") do (
FOR /F "delims=" %%N in (%%a) do COPY "C:\Files\%%N.jpg" %cd%
)
The second For loop works when used in isolation with a hardcoded .txt.
Sorry if it's a newbie mistake, I'm new to cmd line.
Here's an alternative, using just a single loop:
For cmd:
For /F "EOL=? Tokens=*" %G In ('%SystemRoot%\System32\findstr.exe "^" *.txt') Do #Copy /Y "C:\Files\%~G.jpg"
For batch-file:
#For /F "EOL=? Tokens=*" %%G In ('%SystemRoot%\System32\findstr.exe "^" *.txt') Do #Copy /Y "C:\Files\%%~G.jpg"
BTW, your own code should look more like this, (using UseBackQ as advised in the comments), especially if their names contain problematic characters:
For cmd
For %G In (*.txt) Do #For /F "UseBackQ Tokens=*" %H In ("%G") Do #Copy /Y "C:\Files\%~H.jpg"
For batch-file
#For %%G In (*.txt) Do #For /F "UseBackQ Tokens=*" %%H In ("%%G") Do #Copy /Y "C:\Files\%%~H.jpg"
You'll note that in both example types I've omitted the destination directory, which will default to the current directory when you do so.
I found the syntax that works:
For %%a in (*.txt) do (
For /f "delims=" %%N in (%%a) do COPY "C:\Files\%%N.jpg" %cd%
)
So I'm trying to make my own dir command for cmd. So far it is working great, except I want to output the directories and files sorted by file extension, in the way
dir /o:ge
would display (folders first, then files sorted by file extension).
So far, my code looks like this
#echo off
rem Title
echo.
echo CURRENT DIRECTORY [%cd%]
echo.
rem Directories
for /d %%D in (*) do (
echo [DIR] %%~nD
)
rem Files
for %%F in (*) do (
echo %%~nxF
)
#echo on
This produces:
I'm not sure how to approach outputting the files sorted by file extension. I have searched the web and can't find a solution to this problem. I do realize batch script is very limited, but I still want to try and implement this. I have thought of using a for loop and storing all the file extensions into an "array" (if that exists in batch), and then outputting them by
*.fileExtension
Any suggestions?
Cheers,
Derek
As in my comment…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/AD/ON') Do Echo [DIR] %%A
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/A-D/OE') Do Echo %%A
Echo(&Pause>Nul
Alternatively…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Delims=" %%A In ('Dir /OGE/-C'
) Do For /F "Tokens=3*" %%B In ("%%A"
) Do If "%%B"=="<DIR>" (If Not "%%C"=="." If Not "%%C"==".." Echo [DIR] %%C
) Else Echo %%C
Echo(&Pause>Nul
I am trying to rename text files with a different text position.
Example :
20170811191008_marie.txt --> marie_txt_20170811191008
I have a very basic rename batch command to rename files in loop but I am trying to find out how I can change the position of the text within the filename.
Command:
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni txt%%~xi"
Per your wish
for /f "tokens=1* delims=_" %%i in (
'dir /b /a-d *_*.txt ^|findstr /i "^20[0-9]*_.*\.txt$"'
) do echo ren "%%i_%%j" "%%~nj_txt_%%i"
> SO_45673483.cmd
ren "20170811191008_marie.txt" "marie_txt_20170811191008"
EDIT forgot to mention to remove the echo in front of ren to really execute the rename once you are shure it does want you want.
For multiple file extensions, you could always use something like this:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "EOL=_ Tokens=1* Delims=_" %%A In ('Where .:??????????????_*.*') Do (
Set "fx=%%~xB"
Ren "%%A_%%B" "%%~nB_!fx:~1!_%%A" 2>Nul)
You can of course still specify .txt instead of .* but there would be no need to hard code _txt_ into the code following it.
I have some folders which says AA2017-123-TEXT and AA2017-124-TEXTS.
I'm trying to use batch to rename these file to AA2017-123 and AA2017-124 removing the text from the folder name. That is I want only the first 8 characters in a folder name.
I am using windows 7
To do exactly what have been asked for, namely splitting off a certain amount of characters, the following code snippet could be used (extracting the first 10 characters here for example):
for /F "delims=" %%D in ('dir /B /A:D "*"') do (
set "FOLDER=%%D"
setlocal EnableDelayedExpansion
move "!FOLDER!" "!FOLDER:~,10!"
endlocal
)
There are several ways to accomplish your task, IMO the simplest is using the hyphens as delimiters in a for loop.
In the cmd line
for /f "tokens=1,2* delims=-" %a in ('dir /B/ad *-*-*') do #echo ren "%a-%b-%c" "%a-%b"
In a batch
for /f "tokens=1,2* delims=-" %%a in ('dir /B/ad *-*-*') do echo ren "%%a-%%b-%%c" "%%a-%%b"
If the output looks ok remove the echo
This batch line works with full filenames:
for /f "skip=3 delims=*" %%g in (cc_data_3-3-2016_15-37-19.xml cc_data_3-28-2016_0-25-36.xml) do (echo %%g >>tempfile.txt)
This one doesn't when filenames are replaced by a wildcard:
for /f "skip=3 delims=*" %%g in (*.xml) do (echo %%g >>tempfile.txt)
What's wrong and what should I do to make it work? I need the *.xml files. They are in the same folder as the batch file.
for /f takes a filename only. If you want to use a wildcard, you'll need to expand the wildcards yourself. You can use for to do that, just not in one call:
for %%f in (*.xml) do (
for /f "skip=3 delims=*" %%g in (%%f) do (echo %%g >>tempfile.txt)
)