Hello newbie looking for some help. I am trying to write a script to search all files and folders in my drive which have a certain identifier. For example "ID -"
I was using the following to get files and directories listed in a log file:
dir ID * /A:-D-H /B /S >> C:\FileCatalogue.log 2>> C:\FileCatalogue.log
dir ID * /A:D-H /T:C /S >> C:\DirCatalogue.log 2>> C:\DirCatalogue.log
However I want the output to be in 3 tabbed columns:
FILE {tab} DIR {tab} ID
ID123 - YYYY - myfile.txt {tab} C:/tmp/tmp {tab} ID123 - YYYY
etc..
etc...
Any help would be greatly appreciated!
Victor
try this:
for /f "tokens=1*delims=-" %%a in ('dir /a-d/b ID*') do echo %%a-%%b %cd% %%a
#echo off
for /f "delims=" %%A in ('dir /a:-d-h /b /s /t:c ID123*') do for /f "tokens=1,2,* delims=-" %%B in ("%%~nxA") do echo(%%~nxA %%~dpA %%B-%%C
Output
ID123 - YYYY - myfile.txt C:\Users\User\Desktop\ ID123 - YYYY
Related
I have multiple files with the same filename under various subdirectories, for example:
c:\kjhsd\client.txt
c:\ekjjs\client.txt
c:\oiwnk\client.txt
I do not know the middle part of the path represented by random letters above, but the filename is always consistent.
I need a way to use this command: type client.txt (or) more client.txt but to display the content of all text files with the filename "client.txt" under any directory at the same time.
So if:
c:\kjhsd\client.txt contained: hello
c:\ekjjs\client.txt contained: helloworld
c:\oiwnk\client.txt contained: helloagain
After running the single command, I would see:
hello
helloworld
helloagain
Thanks in advance :)
from cmd.exe:
#for /r c:\ %a in ("client.txt") do #type "%~a"2>nul
From a Batch-File:
#for /r c:\ %%a in ("client.txt") do #type "%%~a"2>nul
Edit: as per the newline requirement:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
type "%%~a"2>nul
echo(
)
Popd
or if you want to see which file had what text:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
echo content: "%%~a"
type "%%~a"2>nul
echo(
)
Popd
The easiest approach is to use a for /D loop to find the different directories, and to let find /V "" return the contents of the files, because then a line-break is automatically appended to every file in case there is none in the file itself (opposed to type).
In Command Prompt do this:
#for /D %I in ("D:\some\root\*") do #(< "%~I\client.txt" find /V "") 2> nul
In a batch file do this:
#for /D %%I in ("D:\some\root\*") do #(< "%%~I\client.txt" find /V "") 2> nul
The 2> nul portion suppresses eerror messages if a directory does not contain a file called client.txt.
Below command adds prefix to all files and lists them in out.txt. But
it also lists the folder names
and adds file path which I don't want.
Is it possible the desired output in cmd itself? Any help will be appreciated.
Command:
for /f %f in ('dir /b /s *.*') do echo dt obsolete %f >> out.txt
Current output:
dt obsolete D:\workdir\src1\drafttest2\python
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind\GB_005.seq
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind\GB_005_py.py
Desired output:
dt obsolete GB_005.seq
dt obsolete GB_005_py.py
You can have a go at this from cmdline:
for /f %f in ('dir /b /s *.*') do echo dt "obsolete_%~nxf">>out.txt
or if you want to include the path:
for /f %f in ('dir /b /s *.*') do echo dt "%~dpfobsolete_%~nxf">>out.txt
Not sure if you plan to do this for files only or files and folders, but if files only, then:
for /f %f in ('dir /b /s /a-d *.*') do echo dt "%~dpfobsolete_%~nxf">>out.txt
To run the above from batch file, you need to double up on all the %
As mentioned in my comment as well, from cmd.exe run for /? to see all the help you need on variable expansion.
I am trying to retrieve the name of the newest .txt file in a directory, on Windows 6.1 , C:\Users\KMST
I have tried the following but it does not seem to work.
for /f "delims=" %%x in ('C:\Users\KMST\*.txt /od /b *.*') do #echo %%x
But the error I got is, %%x was unexpected at this time.
From command line the syntax for for command does not need to escape the percent sign used in the replaceable parameter, so
for /f "delims=" %x in ('dir /a-d /b /o-d C:\Users\KMST\*.txt') do #echo %x
but this will list all the files in descending date order.
To only get the newest file from command line you can try with
dir /a-d /o-d /b "C:\Users\KMST\*.txt" | cmd /v /c"set /p.=&&echo(^!.^!"
I have a problem
i need modification date of file. And i use windows batch script. But i can't get modification date and split them.
i currently using this command
for /f %a in ('dir /b') do #echo %a %~ta
result
file89401.txt 06/18/2005 05:37 AM
file8941.txt 11/21/2000 03:48 PM
file89411.txt 09/02/2008 02:14 PM
file89421.txt 03/01/2012 02:06 PM
file89431.txt 10/23/2001 06:48 AM
file89441.txt 10/27/2010 10:21 AM
file89451.txt 11/11/2011 05:40 AM
file89461.txt 11/23/2000 02:48 AM
file89471.txt 10/10/2001 01:28 AM
file89481.txt 07/14/2000 10:17 AM
file89491.txt 02/21/2004 10:24 PM
file89501.txt 03/12/2011 09:42 AM
file8951.txt 09/26/2003 11:31 PM
file89511.txt 08/17/2001 02:31 AM
file89521.txt 01/01/2004 01:11 PM
and i need only modification date and following format
2005/06/18
2000/1/21
2008/09/02
2012/03/01
2001/10/23
2010/10/27
2011/11/11
2000/11/23
2001/10/10
2000/07/14
2004/02/21
2011/03/12
2003/9/26
2001/08/17
2004/01/01
please help me,, sorry my bad English :(
Let's say you want to see modified date of files under directory C:\Users\test in format yyyy/mm/dd, below works perfectly for you:
#echo off
setlocal
for %%F in ("C:\Users\test\*") do (
for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
echo %%C/%%A/%%B
)
)
Result:
2013/12/21
2013/11/19
2012/12/28
2012/10/25
2011/10/25
2011/11/09
2010/12/21
findstr can only output Date/Time, not Date only.
You can fix it with some code or use a command that does what you want:
forfiles /c "cmd /c echo #fdate"
this is not a well knwon command, but it does give you the dates.
For more info see forfiles /?
to format the date to the desired format use:
for /f "tokens=1,2,3 delims=." %%a in (' forfiles /m *.* /c "cmd /c echo #fdate" ') do echo %%c/%%b/%%a
(Attention, this is language-dependent This line is for german format ("25.12.2013")
For your format echo %%c/%%a/%%b should do the trick)
EDIT
just noticed, you want no leading zero on day or month.
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=." %%a in (' forfiles /m *.* /c "cmd /c echo #fdate" ') do (
set dat=%%c/%%a/%%b
set dat=!dat:/0=/!
REM replaced "/0" with "/"
echo %!dat!
)
From the prompt,
for %a in (*.*) do #for /f "delims=/ " %i in ("%~ta") do #echo %k/%i/%j
should deliver the goods.
As a batch file,
#echo off
for %%a in (*.*) do for /f "delims=/ " %%i in ("%%~ta") do echo %%k/%%i/%%j
How do you iterate over each file in a directory with a .bat or .cmd file?
For simplicity please provide an answer that just echoes the filename or file path.
Command line usage:
for /f %f in ('dir /b c:\') do echo %f
Batch file usage:
for /f %%f in ('dir /b c:\') do echo %%f
Update: if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.
for /f "delims=|" %%f in ('dir /b c:\') do echo %%f
Update 2: (quick one year and a half after the original answer :-)) If the directory name itself has a space in the name, you can use the usebackq option on the for:
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
And if you need to use output redirection or command piping, use the escape char (^):
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files" ^| findstr /i microsoft`) do echo %%f
Alternatively, use:
forfiles /s /m *.png /c "cmd /c echo #path"
The forfiles command is available in Windows Vista and up.
Easiest method:
From Command Line, use:
for %f in (*.*) do echo %f
From a Batch File (double up the % percent signs):
for %%f in (*.*) do echo %%f
From a Batch File with folder specified as 1st parameter:
for %%f in (%1\*.*) do echo %%f
Use
for /r path %%var in (*.*) do some_command %%var
with:
path being the starting path.
%%var being some identifier.
*.* being a filemask OR the contents of a variable.
some_command being the command to execute with the path and var concatenated as parameters.
Another way:
for %f in (*.mp4) do call ffmpeg -i "%~f" -vcodec copy -acodec copy "%~nf.avi"
I had some malware that marked all files in a directory as hidden/system/readonly. If anyone else finds themselves in this situation, cd into the directory and run for /f "delims=|" %f in ('forfiles') do attrib -s -h -r %f.