How to write a batch file to create folder.

November 11, 2010 at 11:30:03
Specs: Windows XP

Hi All,

I want to write a batch file which will create a new folder on a particular share drive path and name it with current day date in the format mmddyy

Please help..

Thanks a for your help in advance. :)


See More: How to write a batch file to create folder.

Report •


#1
November 11, 2010 at 11:33:51

If this doesn't work, Go into the prompt and type Date /t and post the results.

MD "P:\a\t\h\%Date:/=%

Can I put a question here?


Report •

#2
November 11, 2010 at 11:40:36

Hi Matt,

The code provided by you creates the folder but it creates the folder with the name.

Fri 11122010 whereelse I want the folder to be named as 111210 .i.e. in mmddyy format.

Thanks a lot for your help in advance.


Report •

#3
November 11, 2010 at 11:46:00

Hi Matt,

I also want to move all files (excluding two files the name of which files I can hardcode in my batch file) from a specific folder present on a share drive to this newly created folder.

Please help.

Thanks a lot for your help in advance.


Report •

Related Solutions

#4
November 11, 2010 at 12:55:31

Try this.

@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "P:\a\t\h\%D%"
COPY "P:\a\t\h\*.*" "P:\a\t\h\%D%"

The first and third "P:\a\t\h" need to be the path to the new folder, and the second one is the path to the files.

Can I put a question here?


Report •

#5
November 11, 2010 at 13:08:13

Hi Matt,

Thanks a lot for your quick reply, It's working fine now as far as creating the directory is concerned but I want to move all files from one directory to another directory and at the same time I want to exclude two files from the source directory while moving it to the newly created directory .i.e. named in format mmddyy. Please help.


Report •

#6
November 11, 2010 at 13:23:51

@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "P:\a\t\h\%D%"

FOR /f %%a IN ('DIR /b "P:\a\t\h\*.*"') DO (
IF NOT "%%a"=="File1" (
IF NOT "%%a"=="File2" ECHO MOVE "%%a"
)
)

For now one, post your full question in the first post, instead of a gneral question, followed by the full question. Also, don;t create a new thread regarding the same issue.

This should do the trick.

Can I put a question here?


Report •

#7
November 17, 2010 at 07:16:46

Hi Matt,

I tried to incorporate the above code in my batch file but it's not working. It doesn't move the files from an another folder to the newly created folder. Here is how I tried it.


@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "\\dv\month\%D%"

FOR /f %%a IN ('DIR /b "\\dv\month\date\*.*"') DO (
IF NOT "%%a"=="abc.zip" (
IF NOT "%%a"=="xyz.zip" ECHO MOVE "%%a"
)
)

Please help..


Report •

#8
November 17, 2010 at 09:32:33

Hi All,

Did anyone get the chance to look into the above post ?.

Thanks a lot for your help in advance.


Report •

#9
November 19, 2010 at 04:24:58

Sorry for the confusion, remove the "ECHO" on line 11.

Can I put a question here?


Report •

#10
November 19, 2010 at 21:36:43

Hi Matt,

Thanks for your reply, As suggested by you I have removed the "ECHO" text from the line 11 but it's still not working. Below is the code :

@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "C:\New Folder\%D%"

FOR /f %%a IN ('DIR /b "C:\New Folder\*.*"') DO (
IF NOT "%%a"=="abc.zip" (
IF NOT "%%a"=="xyz.zip" MOVE "%%a"
)
)

Thanks a lot for your help in advance.


Report •

#11
November 21, 2010 at 08:28:48

Sorry, I must have been tired when I posted that. I forgot to tell it where to move the files.

@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "C:\New Folder\%D%"

FOR /f "tokens=*" %%a IN ('DIR /b "C:\New Folder\*.*"') DO (
IF NOT "%%a"=="abc.zip" (
IF NOT "%%a"=="xyz.zip" MOVE "%%a" "P:\a\t\h"
)

Can I put a question here?


Report •

#12
November 21, 2010 at 19:34:49

Hi Matt,

Thanks a lot for your reply, It's still not working. It creates the folder but doesn't move any files into it.

@ECHO OFF

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "C:\New Folder\%D%"

FOR /f "tokens=*" %%a IN ('DIR /b "C:\New Folder\*.*"') DO (
IF NOT "%%a"=="abc.zip" (
IF NOT "%%a"=="xyz.zip" MOVE "%%a" "C:\New Folder\%D%"
)

Thanks a lot for your help in advance.


Report •

#13
November 21, 2010 at 19:45:40

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "C:\New Folder\!D!"

FOR /f "tokens=*" %%a IN ('DIR /b "C:\New Folder\*.*"') DO (
SET Var=%%a
IF NOT "!Var!"=="abc.zip" CALL :Sub1
)

GOTO :EOF

:Sub1

IF NOT "!Var!"=="xyz.zip" MOVE "!Var!" "C:\New Folder\!D!\!Var!"

GOTO :EOF

Sorry about that, I guess my "if within and if" is flawed. This should work.

Can I put a question here?


Report •

#14
November 21, 2010 at 20:14:33

Hi Matt,

Thanks a lot for your reply, But still there is an issue it misses one file while moving the files to the newly created folder. It also moves the batch file created by me into the newly created folder. for e.g. I have three "xls" files and two zip files in the C:\New Folder\ path. I want to create a new folder with date format as name (which creates now) but it moves two xls files and one xls file remains in the same folder .i.e. New Folder and It moves the batch file to the mmddyy folder created using the same batch file. Please help...


Report •

#15
November 22, 2010 at 03:27:39

Hi Matt,

Did you get the chance to look into the above post ?.

Thanks a lot for your help in advance.



Report •

#16
November 22, 2010 at 04:39:11

It's working fine on my end now, we can come at it from a completely different approach though.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "C:\New Folder\!D!"

FOR /f "tokens=*" %%a IN ('DIR /b "C:\New Folder\*.*"') DO (
SET Num=0
SET Var=%%a
IF NOT "!Var!"=="abc.zip" SET /a Num+=1
IF NOT "!Var!"=="xyz.zip" SET /a Num +=1
IF "!Num!"=="2" MOVE "!Var!" "C:\New Folder\!D!\!Var!"
)

You don't need to put the batch file in the folder you are using, so leave it on your desktop of something.

Can I put a question here?


Report •

#17
November 22, 2010 at 07:19:28

Hi Matt,

It's still not working. I have saved the batch file on my desktop and tried to run the same but still it only creates the folder and doesn't move any files.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET D=%Date:/=%
SET D=%D:~4,4%
SET D=%D%%Date:~-2%

MD "\\sadr\abc\!D!"

FOR /f "tokens=*" %%a IN ('DIR /b "\\sadr\old folder\*.*"') DO (
SET Num=0
SET Var=%%a
IF NOT "!Var!"=="abc.zip" SET /a Num+=1
IF NOT "!Var!"=="xyz.zip" SET /a Num +=1
IF "!Num!"=="2" MOVE "!Var!" "\\sadr\abc\!D!\!Var!"
)

None of the files get move to the newly created folder. Please help..


Report •

#18
November 22, 2010 at 18:24:21

I;m truly sorry, but I totally missed this, put the full path in the in the start location and the final location.

IF "!Num!"=="2" MOVE "\\sadr\old folder\!Var!" "\\sadr\abc\!D!\!Var!"

Change Line 15 to that.

Again, sorry for the confusion.

Can I put a question here?


Report •

#19
November 22, 2010 at 19:38:53

Hi Matt,

Thanks a lot it's working fine now.


Report •


Ask Question