automate .bat to get all content of a given directory - batch-file

I am trying to automate a process that stores all content (directories, sub-directories, files etc.) in a .txt file. I came up with this:
cd "D:\sources\SVN_WorkingCopy\"
dir /s /b>filelist.txt
When putting the above in a .bat file and running it manually, it works fine and creates the .txt file. However, when i include it in a Windows Scheduled task, it doesnt do anything. Can anyone help, getting this to work ?
Thanks!

If you really wanted to do it in two lines then you need to make sure you use the /D option, to change drives too:
CD /D "D:\sources\SVN_WorkingCopy"
Dir /S /B > "filelist.txt"
Alternatively you could use a single line without changing the current working directory:
Dir /S /B "D:\sources\SVN_WorkingCopy" > "filelist.txt"
Or with a different command:
Where /R "D:\sources\SVN_WorkingCopy" * > "filelist.txt"
Also you need to be aware that when run as a scheduled task, the working directory is %SystemRoot%\System32 which is by default write protected, (dependent upon permissions). Therefore the command will try to send its output to a file there unless you provide an accessible location:
> "C:\Users\kalinkula\Desktop\filelist.txt"
You could send the output to a file in the assessed directory, D:\sources\SVN_WorkingCopy\filelist.txt, as long as you don't mind that file appearing in it's own contained listing.
These answers also assume that D: is mapped and/or available at the time the script/command is run.

This is probably just an FYI, but you could also do a structured output using tree
tree /f /a "D:\sources\SVN_WorkingCopy" > filelist.txt
which will result in the file structure to be something like:
D:.
├───e-shopper
│ └───Eshopper
| └───temp folder
| └───Performance Report Table.xlsx
| └───Sales Document.docx
├───test Folder
│ └───Hire Freelancers
| └───Performance Report Table.xlsx
| └───Sales Document.docx
└───Some other Directory with no subs or files

Related

How to only access one additional subdirectory and access their files utilizing batch?

I usually program in Java, but for reasons I won't specify I am trying my hand at solving a problem utilizing batch scripting. I have a couple directories that hold sub directories of days. For example I may have a folder called January and inside that folder are folders for each day (1, 2, 3, etc.) and inside those folders are the text files that I need to access. However, this data is constantly updated so I never have a set number of folders. I am wondering how I can access all the 'day' folders through a batch script?
I have been trying to iterate through the directories and figuring out what folders are inside the overall folder (ie: the month folder) by using the /d /r commands in a for loop. However this only gives me the files in the directory.
Some code snippets that I've tried are:
FOR /D /r %%P IN (..\JAN\Processed\) DO (
copy filex.txt ..\JAN\Processed\%%~nxP
/* run a correlation program using the file located in the directory specified with the copy command */
)
I've also tried to use
for /r "..\JAN\Processed\" %%P in (.) do (/*similar to code above*/)
When I run this code, it will access every directory and not just the ones that I want (because I have additional directories in the 'day' folders).
To test my code I have also used the ECHO command a lot, just to see if it's actually finding the directories I need by putting echo %%~nxP, but it doesn't seem to find the directories I want to access. It'll just return ECHO IS ON/OFF. Your help is much appreciated.
Get a for /d to get the subfolders (only depth=1) and a second nested for to get the files in that subfolder:
#echo off
for /d %%P in ("..\JAN\Processed\*") do (
for %%F in ("%%~fP\*") do echo %%~fF
)

How to differentiate between system and user files in the batch language?

I am looking to copy all text files on the C drive, but the program needs to avoid text files that are part of the system, and only copy the text files that have been created by the user.
Is there a built in way to do this, or do I need to get creative? If so, how would you go about it? I would like to keep it within batch and not involve powershell or anything like that.
I thought about using creation dates to determine whether or not a file is a system file, but that didn't work all the way.
You can generate a list of all systemfiles using the dir command. So
dir /b /as > %USERPROFILE%\excluded_Files.txt
Would generate a list of the systemfiles from that directory.
Using xcopy as described here you can now copy all files from one directory to another excluding the systemfiles.
The xcopy command should be something like this:
xcopy C:\firstDirectory\*.txt C:\secondDirectory /EXCLUDE:%USERPROFILE%\excluded_Files.txt
Edit
So your code that is there would copy all rtf and txt files from the whole C drive to Drive D folder E.
A batch-file using my method would look like this:
#echo off
cd /d C:\
dir /b /as /s > %USERPROFILE%\excluded_Files.txt
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
xcopy C:\*.txt D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
Explanation:
#echo off supresses redundant output of the commands that would be displayed before execution.
cd /d C:\ changes the current execution directory to the root of the C drive. /d is added for potential drive change.
dir /b /as /s > %USERPROFILE%\excluded_Files.txt redirects the output of the dir command into a file in your userprofile (Usually C:\Users\yourUserName). The switch /b will only show the filenames and not size, creation date etc. /as will only list system files and /s makes the dir command work recursively through all directories.
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s will copy all rtf files from C:\ to D:\E and exclude all files from the list previously created (that will contain all system txt and rtf files (as well as others)). /s makes xcopy work recursively as for dir.
Next line is a copy of the same with txt files.

How to delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

Batch File to Delete Specific Subfolders

I have three directories, and each of these directories contains 8 subdirectories, and each of those subdirectories contains 2 folders that I want to delete.
The structure:
[Edited the path-name]
C:/Process/CIM
/AB_CIM
/LOG
/DATA
/CC_CIM
/LOG
/DATA
/CIM_RD
...
...
C:/Process/DIM
/AB_DIM
/LOG
/DATA
/CC_DIM
/LOG
/DATA
/DIM_RD
...
...
C:/Process/RRS
...
So basically I want to remove LOG and DATA in all of those subdirectories. I thought of doing this using two separate batch files***:
The first batch file would contain:
call secondBatchFile.cmd CIM
call secondBatchFile.cmd DIM
call secondBatchFile.cmd RRS
Now, I'm not sure how to write the second batch file since those subdirectories have different names (but a common part: AB_*, CC_*, *_RD, etc). I was wondering if someone could help me with it.
Thanks for your help in advance.
*** The reason for doing this using two separate batch files is that I might sometimes need to keep LOG and DATA for one of the parent directories (e.g. CIM), so this way I can just comment out only one line of the first batch file and then run it.
You could do something like this if you're confident that LOG and DATA folders in other directories won't be picked up. Comment out the actual delete in the code below and review the .dat file output before executing.
REM Output all LOG and DATA sub-directories into corresponding DAT files
dir /ad/s/b log* > log_directories.dat
dir /ad/s/b data* > data_directories.dat
REM remove every entry listed in log_directories.dat
for /f %%i in (log_directories.dat) do rd/s %%i
REM remove every entry listed in data_directories.dat
for /f %%i in (data_directories.dat) do rd/s %%i
If you run this from C:, you're probably going to get directories you don't want. But assuming all of your targets are grouped under a dedicated sub-directory (and assuming you run the .bat from that dedicated directory), this won't be a problem.
And by default, this solution gives you your desired log of which directories it will be deleting (log will be overwritten for each run though).
If the 8 sub-folders are always the same pattern, i.e. AB_*, CC_*, *_RD, etc, the second batch file could be something like:
cd C:\%1%
rmdir AB_%1%\LOG
rmdir AB_%1%\DATA
rmdir CC_%1%\LOG
rmdir CC_%1%\DATA
rmdir %1%_RD\LOG
rmdir %1%_RD\DATA
...
cd c:\

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Resources