In my HTML page, I am showing c*urrent day and last 4 days reports*. I have already designed HTML page. But for every day, I have hard-coded the JPG Image filename.
I am using naming convention as follows -
For Current Day, CurDt_S1
For Current Day minus 1 day, CurDt_1_S1
For Current Day minus 2 days, CurDt_2_S1
For Current Day minus 3 days, CurDt_3_S1
For Current Day minus 4 days, CurDt_4_S1
Now, I want to rename a series of .jpg but i want to append the string at the end of file name. Please help me how can I achive this. Through vbscript or HTML or batch file.
I tried using batch file but it is not working for me.
Series of jpg files - CurDt_S1, CurDt_S2, CurDt_S3 etc.. uptill S20.
CurDt_S1 will be renamed as CurDt_1_S1 uptill S20
CurDt_1_S1 to CurDt_2_S1 uptill S20
CurDt_2_S1 to CurDt_3_S1 uptill S20
CurDt_3_S1 to CurDt_4_S1 uptill S20
CurDt_4_S1 files will be archived to a diff folder or would be deleted
Any SIMPLE solution which i can code is welcome. :)
Thanks.
First off, HTML won't be able to offer anything for this. It is a markup language, not a programming language.
In visual basic, you have a lot of options though. Firstly, you want to be changing the filename, which you can do using
My.Computer.FileSystem.RenameFile(fileX, fileY)
In the case of Cur_Dt_4_s1 you would be using
My.Computer.FileSystem.MoveFile("C:\directory1\file.txt","C:\directory2\file.txt")
To handle the name changes, first create a loop which will run through the folder of these jpegs.
Next, load each file in order, using a method from the path class to get collect the entire path for each file as a string. As you have maintained a standard naming system for your files, all it takes is:
result = Path.GetFileName(fileName); //result being a string
and then, because the string "result" is already an array of chars access the 6th character (your number) by using:
Dim charNum = result(5)
To change 'charNum' to an integer, use
CInt(charNum)
After that it's pretty simple. You'll +1 the variable you made from CInt-ing the number, then converting it back to a character with CChar, pushing it back in to the array in the same position, and then using that new string as the variable you will have for the fileY string.
Hopefully you can explore this a little better yourself, but I will construct the code properly if you are still struggling.
I finally figured out the SIMPLEST way to achieve this bu using BATCH FILE in simplest way.
By Using following code -
del CurDt_5_S*
ren CurDt_4_S* CurDt_5_S*
ren CurDt_3_S* CurDt_4_S*
ren CurDt_2_S* CurDt_3_S*
ren CurDt_1_S* CurDt_2_S*
I tested it and its working. :) :) :)
Related
This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed 1 year ago.
I'm trying to preserve the dates of files that I'm backing up onto an external drive, in the unlikely event that the dates get messed up for whatever reason (I had a previous experience where I lost date information and had no backup). I'm doing this through a batch file containing the following:
#ECHO OFF
cd E:\PCBackup
dir /s > dirlist.txt
I would simply run this batch file after running my backup using FreeFileSync. Then, if I need to, I can search the txt file for the filename and see its corresponding date.
However, when this batch file runs, if there is a previous dirlist.txt, then it is overwritten with the new dirlist.txt. So, in a scenario where the dates get messed up and I don't yet realize it, if I run this batch file, it will overwrite the previous dirlist.txt with one that has the messed up dates, and I'd lose the date information!
So, what I think I want it to do is, if dirlist.txt already exists, then create a new one, say something like dirlist1.txt, so that I can have several "backups" of the text file that I can manually delete if necessary.
I've seen that one can instead use >> with something like dir /s >> dirlist.txt to append to an existing file instead of overwriting, but I don't want to append if I don't have to, I'd still like to create a new file.
Is there a way to accomplish this? I'm also open to alternative/simpler ways of preserving the dates, if there are any. Please keep in mind that I know little about CMD commands or programming, outside of a computer science course I took years ago. Thank you.
You will be told there are umpteen duplicate ways to do this so in this 22 nd year of the 1st century :-) Windows has no native way of returning a sequential Iso Date the primary answer will be use powershell and for my locale it needs to be called in a suitable format, introducing a delay.
powershell get-date -format "{yyyy-MMM-ddTHH_mm+01Z}"
Note:- colons : are not allowed, and for me 20 seconds later on one machine (but it does get faster with use) and 12-5 seconds later on this one, I get
2021-07-07T21_55+01Z
but actually its now 2021-Jul-07 21:56
I have found that the MakeCab method is faster and reliable but again the format is not pure sequencing and the Jul will NOT appear before Dec in a file list without significant batch file processing.
2021-Dec-31 23:00:00.txt
2021-Jul-08 21:54:20.txt
So in a .cmd I prefer a more instant result thus my clock is set to International dates (You will need to look at your LOCALE clock setting bottom right for your own construction.)
set isodate=%date:~0,10%
instantly returns
isodate=2021-07-07 and I can then use that for filename
#ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
dir /s > %isodate%-dirlist.txt
dir returns includes 2021-07-07-dirlist.txt
If you want to run several times in a day use
#ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
set "isotime=%time:~0,2%-%time:~3,2%-%time:~6,2%"
dir /s > %isodate%T%isotime%+01Z-dirlist.txt
Amend that any way you wish for your timezone, thus your own clock whatever your date format be it :-
31/2021/12
look at the way I split %time :~ start#base 0 , # of chars %-
one example for an "English" clock date of 31/12/2021 would be simply reverse to
"isodate=%date:~6,4%-%date:~3,2%-%date:~0,2%"
For American %date%=Thu 07/08/2021 use
"isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%"
Learned batch a while ago in school, haven't ever used it until now b/c I had an idea. Just for fun I wanted to mess around with powercfg batteryreport with my new laptop, but I want to archive. I was going to try and figure out how to have the file powercfg batteryreport spits out changed in some sort of numerical order but I don't even know where to begin, so I decided to just make a new line that takes the current file created and adds the date. All of this is taking place inside of a special folder I created, so pathing isnt necessary.
#echo off
powercfg batteryreport
rename "battery-report.html" "batteryreport %date%.html"
This exact script works without the date variable, but never with it in, but of course I need a variable present in order to have multiple reports saved, as opposed to it writing over itself every time. I've tried messing with all spacings, quotes vs no quotes, no luck. Help (or a better way, preferably with explanation) would be greatly appreciated.
In all likelihood, your date format contains / which is illegal in a filename.
Use %date:/=-% in place of %date%. this converts each / to - (see set /? from the prompt for docco)
Equally, you could use %time::=.% o convert the time to a version usable in a filename.
To remove dayname, you need to use an intermediate variable:
set "dateonly=%date:* =%"
ren ... "...%dateonly:/=-%..."
The * means "all of the characters up to and including the first space" and this is replaced by nothing (the string following the =)
see set /? from the prompt for details.
We have a 2 step process that collects all filenames from a folder into a Word document for use elsewhere.
The original process was to run an old DOS batchfile that collected the filenames into a DOS .txt. Then we launched a Word .docx with a macro that imported the .txt and massaged the formatting. After visual inspection we hit ‘Save’ and that was it.
I had the bright idea that a step could be taken out by launching Word directly from the batch, so I inserted the line: start winword filename. This works great except that the default location that Word wants to save in is now my Templates folder. Running it the old way still works perfectly.
The question is: why is the default location changed by launching Word programmatically and how can it be forced back to the correct location?
Thanks
you can use:
start /D C:\path\to\folder "" winword.exe
this program starts winword.exe and save all files to C:\path\to\folder
ill assume that winword.exe is in the current directory.
for help with the start command : http://ss64.com/nt/start.htm
I investigated the start command, but never did figure out why it operated differently. The eventual solution was to include the Save action in the macro. I still don't know why we didn't have to do that before, but it works now so we're declaring success and moving on.
This is the script I have right now in a .bat file.
copy S:\Stuff\Stuff\"Database1.accdb" S:\Stuff\Stuff\AccessBackUp\AccessMainDB
This works fine, but in the new backup folder, I get a file named "Database1.accdb"
I don't want each backup to be overwritten. How can I add the date to it? I also tried something like this.
copy S:\Stuff\Stuff\"Database1.accdb" S:\Stuff\Stuff\AccessBackUp\AccessMainDB\"Database1.accdb"%time::=%
But nothing happens with this.
Is it possible to do several files using this? If so, how?
set "affix=%date%%time%"
set "affix=%affix::=%"
set "affix=%affix:/=%"
copy S:\Stuff\Stuff\"Database1.accdb" "S:\Stuff\Stuff\AccessBackUp\AccessMainDB\Database1.accdb_%affix%"
This sets affix to the concatenation of the date and time, then removes colons and slashes which are not allowed in filenames. Then append the affix to the destination filename.
The code is likely to need tweaking to remove daynames and other unwanted elements. Without knowing exactly what your date and time formats are, it's not possible to advise. For instance, date may be represented by the three elements in dd/mm/yy or mm/dd/yy or yy/mm/dd order, may have leading zeroes suppressed, may have 2 or 4-digit years numbers, may or may not have a dayname which may be abbreviated or not and may not be in English. There's another set of variables for the time.
In c#, given a folder path, is there a way to get the last modified file without getting all files?
I need to quickly find folders that have been updated after a certain time and if the file that was last modified is before the time, i want to skip the folder entirely.
I noticed that folder's last modified time does not get updated when one of its file get updated so this approach does't work.
No, this is why windows comes with indexing to speed up searching. The NTFS file system wasn't designed with fast searching in mind.
In any case you can monitor file changes which is not difficult to do. If it is possible to allow your program to run in the background and monitor changes then this would work. If you needed past history you could do an initial scan only once and then build up your hierarchy from their. As long as your program is always being ran then it should have a current snapshot and not have to do the slow scan.
You can also use the Window Search itself to find the files. If indexing is available then it's probably as fast as you'll get.
Try this.
DirectoryInfo di = new DirectoryInfo("strPath");
DateTime dt = di.LastWriteTime;
Then you should use
Directory.EnumerateFiles(strPath, "*.*", SearchOption.TopDirectoryOnly);
Then loop the above collection and get FileInfo() for each file.
I don't see a way how can you get the modified date of a file w/o getting reference to FileInfo() on that file.
I don't think FileInfo will get this file as far as I know.