Using Wildcards With Directories or Workaround

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dukeagles21
Posts: 4
Joined: 03 May 2012 12:01

Using Wildcards With Directories or Workaround

#1 Post by dukeagles21 » 03 May 2012 12:15

Hey Everyone,

Bit new to the batch forum game, so I have a situation at hand that I can't find a solution to.

I found how to create a folder via batch with a timestamp (foldername ie: 20120503_140000).

I then would like to copy files into that same folder. I've scoured the internet for ways to do via my original approach (Set another variable equal to the date and then wildcard after :20120503* ) but this is the wrong syntax. It's virtually creating an archive of the day's daily files.

I've looked into for loops and I guess I don't understand the syntax of them enough to figure out my problem.

Thanks for any help

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using Wildcards With Directories or Workaround

#2 Post by Aacini » 03 May 2012 12:40

Excuse me. You had not indicated what is the format of the files you want to copy! Include they the date in its names? In wich part/format?

Welcome to this forum! :mrgreen:

dukeagles21
Posts: 4
Joined: 03 May 2012 12:01

Re: Using Wildcards With Directories or Workaround

#3 Post by dukeagles21 » 03 May 2012 12:47

Thanks for the response Aacini.

The files are all .txt and it changes as to what is in front of them. It's virtually creating a batch archive so *.txt would work. If you're getting specific, dates are involved and the files are date stamped with out the time.

After copying them to the folder, I'm moving them to other folders, but I know how to accomplish that since those files are static.

What else do you need?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using Wildcards With Directories or Workaround

#4 Post by Aacini » 03 May 2012 12:59

I wanted an example of the name format. Suppose that file names have this format: YYYYMMDD_anything.txt. I suppose you have the YYYYMMDD in a variable (I assume is called dateNow). If not, you can separate it from the folder name via a FOR command:

Code: Select all

for /F "delims=_" %%a in ("%foldername%") do set dateNow=%%a
or in an easier way:

Code: Select all

set dateNow=%foldername:~0,8%

The copy you want is achieved this way:

Code: Select all

copy "%dateNow%_*.*" "%foldername%"
If this is not the format of the file names, a slightly modification is needed...

dukeagles21
Posts: 4
Joined: 03 May 2012 12:01

Re: Using Wildcards With Directories or Workaround

#5 Post by dukeagles21 » 03 May 2012 14:22

Format(Can't put exact file name due to privacy reasons) would be as such: CompanyName_Region_Type_%datenow%_Extratext.txt

Looking at the code, I'm still unsure of how the for loop works.
Is delims a set variable in windows? I take it that it's looking for "_" in the folder name.

But how does a copy a work like that? I'm using this code to create the folder:
set FOLDERNAME=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

Since the time will change when I need it to move to Folder, it will not recognize the time and not find the folder.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using Wildcards With Directories or Workaround

#6 Post by Aacini » 03 May 2012 16:28

Ok. Lets do some clarifications. Assume you want to wrote a Batch file that:

1- Create a folder with current date and time.
2- Copy files to that folder.

Code: Select all

set folderName=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
md %folderName%
set dateNow=%FOLDERNAME:~0,8%
copy "*_%dateNow%_*.txt" %folderName%

Now assume you have two Batch files. The first one create the folder, and you want the second one copy files to the previously created folder of the same date:

Code: Select all

for /D %%a in (%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_*) do set folderName=%%a
set dateNow=%FOLDERNAME:~0,8%
copy "*_%dateNow%_*.txt" %folderName%

Note that previous code would copy to the last folder if several folders were created today. Type FOR /? for further details on FOR use. (Question: if just one folder can be created each day, why include the time in its name?)

If you want to specify the date to work over, just replace the %DATE:~10,4%%DATE:~4,2%%DATE:~7,2% part in the FOR command by the given date.

P.S. You will try to be as clear as possible in future posts. I assumed you want to copy files in the same Batch file you created the folder... :|

dukeagles21
Posts: 4
Joined: 03 May 2012 12:01

Re: Using Wildcards With Directories or Workaround

#7 Post by dukeagles21 » 04 May 2012 14:23

Alright, I understand the breakdown of the code. As for having the actual time in it, it's not my call. I'm low on the totem pole.

Thanks for all of your help Aacini. I was completely blanking out on the folder name issue. I thought when the variable was called it would reset itself to the new time, but i was way off.

Much appreciated!

Post Reply