[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 750
  • Last Modified:

Save filenames for files in a directory

I am try to write a script that stores all the files read by my application during a certain batch of processing. All my source files are in a folder called "Processing".
I want to create a "master log" of all files processed during each run. At the end of each run, I want a batch script that will:
      Store all filenames in the processing folder in a file called "Files_Processed.log", ideally formatted like this:

      DATE TIME                  FILENAME
      2009/02/12 07:00:00            A.B.C.D.20090201

I need to be able to do this using only batch scripts. Any help is appreciated.
      
0
mightyestme
Asked:
mightyestme
4 Solutions
 
ShmoidCommented:
Put this in your batch file.
dir   /a-d   /n   >  Files_Processed.log
0
 
mightyestmeAuthor Commented:
But that will add a lot of extra information. I want this log to be as concise as possible. Just the filenames and the datetime when they were processed.

echo %date% %time% >> files_processed.log
dir d:\processing /b /s >> files_processed.log

gives me almost everything I need. I just need to strip the path from the filenames from the second command. Currently, my log files looks like this:

"Thu 02/12/2009" "18:08:33.31"  
c:\detectFeeds\20092301_move_progress.log
c:\detectFeeds\20092302_move_progress.log

I want it to look like:
"Thu 02/12/2009" "18:08:33.31"  
20092301_move_progress.log
20092302_move_progress.log

I want to remove the folder name and path from the dir /b /s command. How do I do that?
0
 
deroodeCommented:
Skip the /s (it adds the directory name)

dir d:\processing /b >> files_processed.log

(or dir d:\detectfeeds /b /s >> files_processed.log )
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
AmazingTechCommented:
What about this?
echo %date% %time% >> files_processed.log
for /f %%a in ('dir d:\processing /b /s') do echo %%~nxa>> files_processed.log 

Open in new window

0
 
BillDLCommented:
You want the File Names as the 2nd of a tab-separated "column" where the Time and Date should be in the first "column" for each of the files listed?

That is what is implied from your "ideally formatted like this" layout in the question, but then you later say that you want the output so that the date and time precedes each separate listing.

If you want to output the formatting as shown in your original question, then try the FIRST batch file in the code snippet.  Notice the two spaces between %DATE% and %TIME% and 4 spaces between %TIME% and %%a ?  Preserve that and you'll get a reasonable layout, or else add or reduce the spaces as desired.

You only want to list the files in ONE folder, don't you?  Again that was stated in your original question, so we will assume so.

If you need to list files in the sub-folders, then use AmazingTech's code combined with mine shown in the SECOND batch file in the Code Snippet.

@echo off
for /f %%a in ('dir d:\processing /b') do echo %DATE%  %TIME%    %%a>> files_processed.log 
exit
 
 
@echo off
for /f %%a in ('dir d:\processing /b /s') do echo %DATE%  %TIME%    %%~nxa>> files_processed.log 
exit

Open in new window

0
 
BillDLCommented:
If there ARE sub-folders of your folder named "processed", then I assume that you will NOT want them in your listing.  You should therefore incorporate Shmoid's suggested /a-d switch.  That tells it NOT to list the names of sub-folders.

eg. For the first batch file in my code snippet:

'dir d:\processing /a-d /b'
0
 
BillDLCommented:
Or why not output it as a *.CSV file that you can then open in Microsoft Excel?

That way you can sort and filter the data and save as *.xls, or import to other programs, or whatever.

@echo off
for /f %%a in ('dir d:\processing /a-d /b') do echo %DATE%,%TIME%,%%a>> files_processed_log.csv 
exit

Open in new window

0
 
mightyestmeAuthor Commented:
THanks a bunch BillDL. Your solutions were the most useful, and the one that I implemented, so 150 points to you. I have distributed all remaining points among the others. Cheers.
0
 
BillDLCommented:
Thank you mightyestme
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now