# Solved: Variable as folder name



## dampyr (Nov 25, 2011)

Hello,
Every first day of the month,we need to create folder for example:11 Novembar
where 11:Number of month,November name of the month:
Is there way to create variable to automatically change folder name accordingly every first of the month:

12 December
01 January
and so on:

Thanks


----------



## Squashman (Apr 4, 2003)

If you are referring to doing it in a batch file then yes you could. Date is a system variable.

```
C:\Users\Squash>echo %date%
Fri 11/25/2011
```
Now if I want to pull the month from that I would do the following

```
C:\Users\Squash>echo %date:~4,2%
11
```
How the date variable outputs in a batch file is dependent on your Regional and Language settings. So yours may be slightly different than mine but you can adjust the code above accordingly.


----------



## dampyr (Nov 25, 2011)

Folder name is in format "11 Novembar" and must be created only 1st day of the month,for example
01.11 11 November
01.12 12 December

...........


----------



## Squashman (Apr 4, 2003)

I don't see the point of creating the month as a numeric value and the Name of the month. If you only need it created the 1st day of the month then use Windows Task Scheduler to only run the batch file the 1st day of the month.

Or if you have an existing batch file use an IF statement to see if it is the 1st day of the month by using the same type of code I posted in my previous post and it if is then create the folder. Of course you will need a bunch of additional IF statements so that you can get your Month Number Month Name format.

I would really need to see your entire batch file to see what is going on to make a better judgment on how to proceed.


----------



## dampyr (Nov 25, 2011)

I will provide it tommorow:

What is the point:In folder "11 Novembar",using batch file-automatically,every morning is created folder name
01.11,02.11,03.11,04,11.....In these folders are files copied from network drive.These files are formated,converted to excel and then sent as mail attachments.All this is attained by macros.When next month come,then manually we must create new folder "12 December" in which all files for december are stored and so on.All works automatically except making new folder every month.In existing batch file,on the beginning,is
11 November,so we change it in 12 December,01 January.I will post batch file as soon as possible


----------



## Squashman (Apr 4, 2003)

So you have some other script that is coded to look at the Month as a Numeric Value and Name Vaue. Why not make it easier on yourselves and change that code to just look for the Numeric value of the month.

I am not really understanding your explanation in your last post. I am going to assume that is a language barrier issue. The way you write your sentences tells me that English may not be your first language.

If you could show me the folder structure I may be able to better understand what you are trying to do.


----------



## dampyr (Nov 25, 2011)

Yes,english is not my naitive language:
In folder "11 November",every morning is created folder for that date:
on the first of november,folder "01.11" is created using batch,on the second of the november,folder 02.11 is created,and so on...

01.11.2011 C:\Local Data\11 November\01.11\some files
02.11 2011 C:\Local Data\11 November\02.11\some files
30.11 2011 C:\Local Data\11 November\30.11\some files

01.12.2011 C:\Local Data\12 December\01.12\some files
31.12 2011 C:\Local Data\12 December\31.12\some files

01.01.2012 C:\Local Data\01 January\01.01\some files


Only part of the code: "11 November" must be manually renamed every month in batch file (01.12 will be renamed to "12 December") in order for folder "12 December" to be created,and that files for december not end up in folder "11 November".

Sorry,i wasn't made myself clear:batch creates folder "11 November" we ONLY need to change "11 November",on the beginning of the next month to:12 December-manually.
If renaming wouldn't be performed,files for oncoming month would be in folder "11 November"


I want for batch itself to change folder name "11 November" to "12 December"-01.12.2011

I hope that is now more obvious what i want.

Batch file runs every morning (5 AM)

When i post batch file content,everything will be clarified


Folder naming convenction CAN'T be changed-company policy


----------



## Squashman (Apr 4, 2003)

That is really overkill and redundant for your folder names. Why do you do it that way?
If I was doing what you are doing I would be doing it more like this:
C:\source\2011\11\25\
C:\source\2011\11\26\


----------



## dampyr (Nov 25, 2011)

Don't ask ,as i said in previous post,IT policy doesn't allow changing naming policy.



@echo off
:: variables
set drive=C:\Local Data\Liste\11 Novembar\
set hc=HC_data\
set ST42=Uzice
set folder=%date:~0,5%
set broj="0610"+%date:~2,2%+".jdc"
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y


----------



## Squashman (Apr 4, 2003)

See how you are creating a substring variable of the system date variable. You can do the same thing with your drive variable but you will also need to use some IF statements to hard code the month name.

Like I said earlier, the date variable is defined by your regional settings. I would need to see the output of the date variable to get the exact code for you but it could look something like this if the numeric digits for the month are the first characters from the date variable.

```
If "%date:~0,2%"=="11" set drive=C:\Locale Data\Liste\11 November\
```
Now obviously you will need 11 more If statements like that.


----------



## dampyr (Nov 25, 2011)

Thank You so much for You effort !!

output for data variable:26.11.2011


----------



## dampyr (Nov 25, 2011)

Here is result from other forum:

If you ran this script with a scheduled task at the beginning of every month, it would create the folder you want on the c drive. If you run it right now, it will create a folder named c:\11_November
@echo off
setlocal
set v=%date:~4,2%
set month_num=%date:~4,2%
set month_text=01-January;02-February;03-March;04-April;05-May;06-June;07-July;08-August;09-September;10-October;11-November;12-December
call set v=%%month_text:*%v%-=%%
set v=%v:;=&rem.%
mkdir c:\%month_num%_%v%
endloca


----------



## Squashman (Apr 4, 2003)

Looks like you are good to go then. But instead of having a separate batch file that is scheduled to run on first day of the month you could just put some code into your existing batch file to see if it is the first day of the month and then just create the directory. Either way works. Just depends on what you feel comfortable with.


----------



## dampyr (Nov 25, 2011)

I would prefer to put this code in existing batch.I really appreciate Your help.Now i must experiment with this code to see if it works as expected.Once again,thanks a lot!!!.You are a script guru!!!


----------



## Squashman (Apr 4, 2003)

I think this will work for you. I kind of took the same idea of how they were setting up the folder name for the month but I figured I could do it in less code than they did.


```
@echo off
:: variables
[color=green]:: setting v to equal the current numeric month[/color]
[color=red]set v=%date:~3,2%[/color]
[color=green]:: putting all the possible months into a variable which will be parsed in the for loop[/color]
[color=red]set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 June" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"[/color]
[color=green]:: for loop is parsing the month_text variable and using the find command with the numeric month to set the current_month variable[/color]
[color=red]for %%I in (%month_text%) do echo %%I |find "%v%" &&set current_month=%%~I
set drive=C:\Local Data\Liste\%current_month%\[/color]
[color=green]:: checking if it is the 1st day of the month and if it is create the new directory.[/color]
[color=red]IF "%date:~0,2%=="01" mkdir "%drive%"[/color]
set hc=HC_data\
set ST42=Uzice
set folder=%date:~0,5%
set broj="0610"+%date:~2,2%+".jdc"
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
```


----------



## dampyr (Nov 25, 2011)

Thanks again,i changed the time in BIOS to 01.11.2011,and script says next:

C:\Users\dampyr>@ echo off
:: variables
:: setting v to equal the current numeric month
set v=%date:~7,2%
:: putting all the possible months into a variable which will be parsed in the f
or loop
set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 Jun
e" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"

for %%I in (%month_text%) do echo %%I |find "%v%" && set current_month=%%~I
*%%I was unexpected at this time.*
set drive=C:\Local Data\Liste\%current_month%\

IF "%date:~4,2%=="01" mkdir "%drive%"
*Data\Liste\%current_month%\" was unexpected at this time.*

I had to change to "%date:~4,2%",this format gives me the day of the month:

C:\Users\dampyr>echo %date:~4,2%
01

month:

C:\Users\dampyr>echo %date:~7,2%
11

Just for curiousity:
what "I" means in "for %%I"?


----------



## Squashman (Apr 4, 2003)

Bios date has nothing to do with the date variable that windows provides. The date variable is determined by your Regional and Language settings which I have told you several times already in this thread. I had the batch file setup based on what you told me outputs from the date variable at the command prompt. You told me the date outputs as DD.MM.YYYY. So my code is setup to look for the day as the first two positions of the date variable and the month as the 4th and 5th positions of the date variable. You also had your Folder variable set as the first 5 positions. So I am not sure why you changed the code around now. You told me in a previous post that your IT policy forbade you from changing the date format.

For Loops use a single alpha character as the variable it uses in the for loop. So "I" is the variable name.


----------



## dampyr (Nov 25, 2011)

That's why i changed:
C:\Users\hacker>dampyr %date:~0,2%
??

And i changed BIOS date to first day of the month in order to see if batch works,
also i first tried with Your initial script,before any changes,but same error appears:

C:\Users\dampyr>@echo off
set v=%date:~3,2%

set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 Jun
e" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"

:: for loop is parsing the month_text variable and using the find command with t
he numeric month to set the current_month variable
for %%I in (%month_text%) do echo %%I |find "%v%" &&set current_month=%%~I
%%I was unexpected at this time.
set drive=C:\Local Data\Liste\%current_month%\

IF "%date:~0,2%=="01" mkdir "%drive%"
Data\Liste\%current_month%\" was unexpected at this time.

I tested this from my home computer,(I created Local Data\Liste folders)

And yes,date format on my home and job computer is in format DD.MM.YYYY
and this batch,for some reason doesn't creates new folder in "C:\Local Data\Liste\"


----------



## Squashman (Apr 4, 2003)

Are you trying to copy and paste all the code I gave you into the command prompt?

Please do this for me.
At the command prompt please type:

```
echo %date%
```
Please copy and paste that output to the forums.


----------



## Squashman (Apr 4, 2003)

Squashman said:


> Are you trying to copy and paste all the code I gave you into the command prompt?


That was actually a rhetorical question. I realize that is what you are trying to do and that doesn't work. You need put the code into a Batch file and then execute the batch file.

I would also appreciate it if you would put your code inside code tags like I have been doing. This helps me distinguish between your code and other comments you are making.
Please see the BBCode tag reference website on how to do that.
http://www.bbcode.org/reference.php

If you quote one of my posts above with code in it you can also see how the CODE tags are used.


----------



## dampyr (Nov 25, 2011)

```
C:\Users\dampyr>echo %date%
??? 27.11.2011
```
I first put Your code into batch file and folder was not created,then i put @pause at the end of batch,and @echo on to see error message,but batch closes quickly so i cannot see the output.That's why i put whole code in cmd-to see error message.And yes,i run batch as administrator


----------



## dampyr (Nov 25, 2011)

```
@echo off

[COLOR=Black]set v=%date:~3,2%

[/COLOR]  [COLOR=Black]set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 June" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"

[/COLOR]  [COLOR=Black]for %%I in (%month_text%) do echo %%I |find "%v%" &&set current_month=%%~I
set drive=C:\Local Data\Liste\%current_month%\

[/COLOR]  [COLOR=Black]IF "%date:~4,2%=="01" mkdir "%drive%"
```
[/COLOR]


----------



## Squashman (Apr 4, 2003)

dampyr said:


> ```
> C:\Users\dampyr>echo %date%
> ??? 27.11.2011
> ```
> I first put Your code into batch file and folder was not created,then i put @pause at the end of batch,and @echo on to see error message,but batch closes quickly so i cannot see the output.That's why i put whole code in cmd-to see error message.And yes,i run batch as administrator


Of course it is not going to create the folder. It will only create it on the first day of the month. That code is obvious with the IF statement. Today is the 27th and 27 does not equal 01.


----------



## Squashman (Apr 4, 2003)

I see a small error in the code. Typical. I didn't have the date variable surrounded in Quotes in the IF command. There should be a quote before the double equal signs. But that got me thinking. You really don't need to check if it is the first day of the month to create the directory. You just need to check if it exists and if it does not exist then create it. So I am going to change the code to the following. See code change in red.

```
@echo off
:: variables
:: setting v to equal the current numeric month
set v=%date:~7,2%

:: putting all the possible months into a variable which will be parsed in the for loop
set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 June" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"

:: for loop is parsing the month_text variable and using the find command with the numeric month to set the current_month variable
for %%I in (%month_text%) do echo %%I |find "%v%" &&set current_month=%%~I
set drive=C:\Local Data\Liste\%current_month%\

:: checking if current month folder exists.  If not create it.
[color=red]IF NOT EXIST "%drive%" mkdir "%drive%"[/color]
```


----------



## dampyr (Nov 25, 2011)

Squashman said:


> Of course it is not going to create the folder. It will only create it on the first day of the month. That code is obvious with the IF statement. Today is the 27th and 27 does not equal 01.


That's why i changed system date to the first of the november in BIOS,to see if this works.Right now i cannot test Your modified code.As soon as i test it,results will be posted in this forum.Thank You so much for Your help


----------



## Squashman (Apr 4, 2003)

dampyr said:


> That's why i changed system date to the first of the november in BIOS,to see if this works.


That is just a bad idea. Changing the system date can wreak havoc especially if you are using a Network file system. You start screwing around with file date and times and I guarantee you will have problems. The easiest way to test it would have been to do what I did and change the code to the current day to force it to create the directory.

Both sets of code works. Some times it is just a matter of troubleshooting why it is not working on your system. When you start talking about using the Date variable it gets really touchy because everyone may be using a different regional setting for their time and date. Now in theory you can interrogate the registry to see how the time and date are formatted and then put the date into defined variables but that adds a whole lot of code to your batch file and you probably wouldn't understand how it works anyways so I wasn't going to bother trying to explain that to you.


----------



## dampyr (Nov 25, 2011)

Uffff!,it finally works !!!!!I couldn't resist so i changed date to 1 december and this works either.Two folders exist:11 November and 12 December.

But it's strange why script with checking date of the month as variable didn't work:

```
[COLOR=Black]IF "%date:~4,2%=="01" mkdir "%drive%"[/COLOR]
```
 Maybe as You said,because regional data settings?

@Squashman Thank You so much for Your precious help!!!!!!.

May i request help from this forum in the future?

This thread could be tagged as solved.


----------



## Squashman (Apr 4, 2003)

dampyr said:


> But it's strange why script with checking date of the month as variable didn't work:
> 
> ```
> [COLOR=Black]IF "%date:~4,2%=="01" mkdir "%drive%"[/COLOR]
> ...


I told you why it didn't work. There is a missing quote that is suppose to surround the DATE variable.

```
IF "%date:~4,2%[color=red][b]"[/b][/color]=="01" mkdir "%drive%"
```



dampyr said:


> May i request help from this forum in the future?


You certainly can but I also like to request that people who have been helped come back here and help others. There are lots of of forum categories here and I am sure you can probably answer some of the questions that are asked in them. Or if you don't feel like helping then please make a small money contribution to help keep the website running.
http://www.techguy.org/donate.html



dampyr said:


> This thread could be tagged as solved.


You can do that yourself. Look above and to the right of your very first post. Use the Thread Tools link to mark your problem Solved.


----------

