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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
This would help me a lot of possible.
Example:
Folder1 has 900 files ending in .zip.
Folder2 has 300 files that match the ones in Folder1 but in .doc
How can I create a batch script to move files from Folder1 to Folder1-sorted that only have a .doc alternative ending up with a folder1-sorted with only 300 .zip files?
My initial guess was doing dir > list.txt and changing the extension of every line and using this list to copy.. but I guess there might be a more clever solution to doing this.
With the aid of Setlocal EnableDelayedExpansion and For loops you could build an indexed array whose values contain just the filenames for your .doc files, using the ~n variable modifier, then use another for loop on your .zip files to compare against the array with a nested For /L loop.
EG:
#ECHO OFF & Setlocal EnableDelayedExpansion
CD "your path for parent directory containing folders 1 and 2"
REM build the array using the smaller data set...
PUSHD Folder2
Set "_I=0"
For %%A in (*.doc) Do (
Set /A _I+=1
Set "file[!_I!]=%%~nA"
)
POPD
PUSHD Folder1
For %%A in (*.zip) Do (
For /L %%B in (1,1,!_I!) Do (
If "!file[%%B]!"=="%%~nA" (
ECHO(%%A matches !file[%%B]!.doc
REM command for on Match here
)
)
)
POPD
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I think this should be rather simple but I want to delete all the files that are stated in for example: C:\TEST but I want to leave the files that are located in subdirectories of this folder. For example files in the folder: C:\TEST\Backup should not be deleted.
When using the following batch command all the files get deleted including those located in sub directories but it leaves the folder:
DEL /S C:\TEST\ /q
Does anyone know the command that I need?
It is as simple as this:
from cmdline:
for %a IN (C:\TEST\*.*) do echo "%a"
OR In a batch script, just add an additional %
for %%a IN (C:\TEST\*.*) do echo "%%a"
Just replace echo with del once you are confident with your final script.
OR simply doing:
del C:\TEST /q
All these things can be found by simply opening cmd.exe and running ANY of the commands with the /? switch. For instance.
for /?
del /?
etc.
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 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 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 8 years ago.
Improve this question
i plan on releasing a tool to back up gamesaves for a certian game but i am having problems with batch telling it to get its text. i get returned with Wput but i want it to say ftp://SPECIFIED_USER_IN TEXT FILE:
Not quite sure if this is possible but i looked around(UPLOAD represents wput.exe) UPLOAD isnt the issue. the issue is telling it where to go.
#echo off
set /p user=./server_information/user.txt
set /p pass=./server_information/pass.txt
set /p host=./server_information/host.txt
ser world=worldname.txt
cd UploadingHandler
upload ftp://%user%:%pass#%host%:21/%world% %world%
pause
any help? Thanks.
edit:
if you need better understanding here is a treemap:
ROOT
UploadingHandler
epload.exe
-
Server_Information
Host.txt
user.txt
pass.txt
-
The SET /P command reads input from the console unless you redirect the input to be from another device, e.g. from a file. This is done using the < symbol:
SET /P variable=prompt text < filename
The prompt text part is optional. So, to give you an example using one of your files:
set /p user=<./server_information/user.txt
Not sure if this is what you wanted either, but to read a file using batch you could do this:
for %%a in (myfile.txt) do print %%a
or:
for %%a in (myfile.txt) do :adef
:adef
print %%a