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
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 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)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I have some files in a some unix server. When i copy to windows, based on filename , the file has to be copied into corresponding year,corresponding month and corresponding date folders. Sample filename : 20120201.117_visual_sciences_web_feed.out.gz.
Folder structure to be created based on first part of filename, in this case : 20120201(YYYY,MM,DD) . In above example ,filename should be copied into 2012 -> 02-> 01 folder.Folders should be created if not created Honestly i am not getting how this can be implemented, please suggest.
This should do the trick for you -
#echo off
set filename=20120201.117_visual_sciences_web_feed.out.gz
set filepath=PATH\TO\%filename%
REM get the date from the file name
for /f "delims=." %%A in ("%filename%") do set fdate=%%A
REM Y=YEAR; M=MONTH; D=DAY
set Y=%fdate:~0,4%
set M=%fdate:~4,2%
set D=%fdate:~6,2%
REM echo %Y% %M% %D%
REM check to see if directories exist
if not exist \%Y% mkdir %Y%
if not exist \%Y%\%M% mkdir %Y%\%M%
if not exist \%Y%\%M%\%D% mkdir %Y%\%M%\%D%
REM copy the file to the newly created destination folder
copy %filepath% %Y%\%M%\%D%\%filename%
pause
I hope you're able to implement this into your script easily enough. As pointed out; if you added some examples from your script - I would be able to help more.
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 9 years ago.
Improve this question
In the end, I want to copy the last modified folder in a directory. In order to do this, I need to pass in the name of the last modified folder to xcopy.
How do you find the last modified folder, not file, in a directory with command prompt? I have found many scripts that will find the last modified file, but I cannot seem to find a command that will find the last modified folder.
Any help would be appreciated.
#echo off
for /f "delims=" %%a in (' dir /ad /od /b ') do set "folder=%%a"
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.
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
In Dos if you type
copy c:\a.txt
it will copy a.txt* (a.txt1, a.txtb, etc)
how can I just copy a.txt?
Your question is not correct - copy c:\a.txt will only copy the single file to the current directory. It will ignore the other files like a.txt1 and a.txtb.
You must have tried copy c:\*.txt - that will copy all forms because the pattern matching searches both long and short (8.3) names.
You can eliminate the problem by using FINDSTR:
for /f "eol=: delims=" %F in ('dir /b /a-d c:\*.txt^|findstr /eli ".txt"') do #copy "c:\%F"