Issue with %date% and %time% on the command line - windows

I got this command to generate a log file that contains the date in the name.
echo %time%;stuff done >> C:\myfile_%date%.log
Output:
The system cannot find the path specified.
If I do remove the system variable, all fine.
I have attempted to add "" and `` and '', no go so far.
Some help would be welcome :)
Thanks.

You need to generate a date without spaces in it. %date% outputs Mon 16/06/2014 which when passed as a filename causes problems with the creation. If you substring each of the date elements and build a custom filename with the below:
echo %time%;stuff done >> C:\myfile_%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.log
your command will work. The filename I get as a result is:
myfile_201416062105.log
which as a backwards timestamp will also make them easier to sort and find after the generation.

Related

unable to call a stored variable in shell script

I have a very basic script that starts with saving part of input argument to a varialbe:
dirN= basename $1
echo $dirN
$dirN was displayed as expected
then I try to cat a string with $dirN
tmp="/some/path/$dirN"
when I echo $tmp, it only displays /some/path/
I tried overwriting tmp
tmp=$dirN
and echo $tmp,
it shows nothing.
it's like $dirN was never stored, but it was echoed from line 2
I am very confused, so was my colleague.
Any hint?
Thank you all!
I believe you want
dirN=`basename $1`
to actually store the value returned instead of
dirN= basename $1
The "echo $dirN" is not showing anything in your version, it's the previous line that shows the output you attribute to the echo.

Renaming file with Batch variable

I have the following code:
set DATE=%date%
echo %DATE%
set DAY=%DATE:~0,2%
echo %DAY%
rename file09.txt file%DAY%09.txt
pause
It is supposed to rename a text file and put the day of the month in the file name. I am however getting a syntax error on the rename command.
I think the problem is in inserting the variable into the file name. Any help would be appreciated. The echos are just in the program for my own reference.
As has already been stated - your code as pasted had trailing spaces on many lines and the spaces often matter. I posted this because you had reused the system DATE variable name and that causes issues, and to show you that quotes are often helpful - and needed with long filenames.
set "D=%date%"
echo "%D%"
set "DAY=%D:~0,2%"
echo "%DAY%"
rename "file09.txt" "file%DAY%09.txt"
pause
you might have trailing spaces in the set command. Just try this:
set "DAY=%DATE:~0,2%"
btw. with set DATE=%date% you doesn't create a new variable. Variables must have case insensitive unique Names.
The problem was the date configuration, because of that the content of the variables is wrong.
Really use quotation marks worked as it ensures full name if the variables have spaces or unusual characters.
Review my post for view the Endoro answer..it works right for me
Renaming file with Batch variable doesnt work with another user profile
"Renaming file with Batch variable doesnt work with another user profile"

Retrieving File name for bash/shell programing

I need to access two files in my shell script. The only issue is , I am not sure what the file name is going to be as it is system generated.A part of the file name is always constant , but the rest of it is system generated , hence may vary. I am not sure how to access these files?
Sample File Names
Type 1
MyFile1.yyyy-mm-dd_xx:yy:zz.log
In this case , I know MyFile1 portion is a constant for all the files, the other portion varies based on date and time. I can use date +%Y-%m-%d to get till MyFile1.yyyy-mm-dd_ but I am not sure how to select the correct file. Please note each day will have just one file of the kind. In unix the below command gives me the correct file .
unix> ls MyFile1.yyyy-mm-dd*
Type 2
MyFile2.yyyymmddxxyyxx.RandomText.SomeNumber.txt
In this file , as you can see Myfile2 portion is common,I can user Date +%Y%m%d to get till (current date) MyFile2.yyyymmdd, again not very clear how to go on from there .In unix the below command gives me the correct file .Also I need to have previous date in the dd column for File 2.
unix> ls MyFile2.yyyymmdd*
basically looking for the following line in my shell script
#!/bin/ksh
timeA=$(date +%Y-%m-%d)
timeB=$(date +%Y%m)
sysD=$(date +%d)
sysD=$((sysD-1))
filename1=($Home/folder/MyFile1.$timeA*)
filename2=($Home/folder/MyFile2.$timeB$sysD*)
Just not sure how to get the RHS for these two files.
The result when running the above scripts is as below
Script.ksh[8]: syntax error at line 8 : `(' unexpected
Perhaps this
$ file=(MyFile1.yyyy-mm-dd*)
$ echo $file
MyFile1.yyyy-mm-dd_xx:yy:zz.log
It should be noted that you must declare variables in this manner
foo=123
NOT
foo = 123
Notice carefully, bad
filename1=$($HOME/folder/MyFile1.$timeA*)
good
filename1=($HOME/folder/MyFile1.$timeA*)

KSH: How to add Date Time at the end of a New File e.g. FileMMDDYYYYHHMM

I am having a requirement where I want the output as
FileMMDDYYYYHHMM
I tried the following things
I am having a file called abc.ksh in which I have Declared a variable as
MYDATETIME=${DATE}${TIME}
and I am passing the parameter as
cp $MY_DATA_DIR/My_Reports $MY_DATA_DIR/My_Reports$MYDATETIME
In the above I am getting the OutPut as My_Reports without MYDATETIME
I also used the echo command to Print the out put
echo "my datetime" $MYDATETIME
I got the Output as
my datetime
It is not adding the expected date time to File [My_Reports].
Am I doing something wrong here?
Can anyone suggest or guide me on the same.
Like others have said, I don't see you initializing your DATETIME variable. If I was in your shoes, I'd do the following:
$MY_DATA_DIR=/this/is/my/derp
MYDATETIME=`date +%m%d%Y%H%M`
cp $MY_DATA_DIR/My_Reports $MY_DATA_DIR/My_Reports$MYDATETIME
If you want to split date and time, then:
$DATE=`date +%m%d%Y`
$TIME=`date +%H%M`
$MYDATETIME = ${DATE}${TIME}
Enjoy!

How to redirect output to a file which name is the current date and time?

I would like to redirect the output of a command (in the Windows command line) to a file which name is the current date and time. For example:
my_path\mysqldump.exe my_database_name > auto_generated_file_name
where auto_generated_file_name should be something like 2010_09_30___11_41_58.txt.
This command will automatically run from time to time. This is the reason I need the file name to be automatically generated.
What is the easiest method to achieve this ?
The following command creates a blank file with the expected filename:
> type nul > %date:~10,4%_%date:~4,2%_%date:~7,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%.txt
> dir /b
2010_09_29__22_12_44.txt
You can use the part after type nul > in place of your auto_generated_file_name.
There's a nice solution here, and it almost matches your example formatting:
set dd = %date% %Time%
my_command > MyFile__%dd:~0,2%_%dd:~3,2%_%dd:~6,4%___%dd:~11,2%_%dd:~14,2%.txt
Output: "MyFile__22_05_2009__6_20.txt"

Resources