Search This Blog

Showing posts with label DOS Batch File. Show all posts
Showing posts with label DOS Batch File. Show all posts

Friday, July 14, 2023

Zipping files with 7z.exe

I continue to work on the Disaster Recovery documentation at work. For future reference, this is the DOS batch file I run each morning to back up my RoboHelp projects that reside in my c:\rh folder. I reduced the font size to avoid ugly line breaks - you are welcome! <grin>
cd\
c:
cd rh
REM | BROKEN COMMAND: for /d %%X in (*) do "I:\7-Zip-Command-Line\7za.exe" a "%%X.zip" "%%X\" -mx=5
REM | BROKEN Command: for /d %%X in (*) do "f:\7-Zip-Command-Line\7za.exe" a "%%X.zip" "%%X\" -mx=5
REM | BROKEN Command below this line:
REM | for /d %X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%X.zip" "%X\"
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\" -mx=5 –tzip
SET %Today%=%Date:~10,4%_%Date:~4,2%_%Date:~7,2%
ren dr-doc.zip %TODAY%_H-rh-dr-doc_Dr-doc.zip
md "s:\technical documentation\_storage\2023\%TODAY%"
move *.zip "s:\technical documentation\_storage\2023\%TODAY%"
cd\
c:
cd rh-2022
REM | BROKEN Command: for /d %%X in (*) do "f:\7-Zip-Command-Line\7za.exe" a "%%X.zip" "%%X\" -mx=5
REM | UNBROKEN Command below this line:
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\" -mx=5 –tzip
ren dr-doc_rh2022.zip %TODAY%_dr-doc_rh2022.zip
move *.zip "s:\technical documentation\_storage\2023\%TODAY%"
pause
md "s:\technical documentation\_storage\2023\%TODAY%\Templates"
cd\
c:
xcopy c:\Users\prhanson\Desktop\Templates\*.* /s "s:\technical documentation\_storage\2023\%TODAY%\Templates"


There was a period in time where my DOS batch file was not functioning correctly so I used the following resource to correct it:
https://stackoverflow.com/questions/34602813/zipping-files-with-7z-exe - Zipping files with 7z.exe

Wednesday, March 15, 2023

Super Super Duper Duper IMPORTANT!! - How to get files out of all subfolders and move them up to the first folder

OH!

MY!

GOSH!

I had no idea I was looking for this information until I found it. How to get files out of all subfolders and move them up to the first folder

This is the relevant information:


From How to Copy Files from Multiple Sub-Folders to a Single Folder?

Open a Command Prompt ('cmd.exe') window. Use the following commands:

cd /d "d:\vacation snaps\2016"

for /r %d in (*) do copy "%d" "d:\all snaps"

In the above example, the command recursively copies all files in the 'vacation snaps\2016' folder to the 'All Snaps' folder.


Monday, September 13, 2021

Using the FOR Statement

From Using the FOR Statement:

The FOR statement is a command that allows you to "loop" or repeat commands multiple times. It is very similar to the FOR statement provided in several programming languages. The FOR statement is often used in batch files, but it can also be used directly on the command line.

As a reminder, the general form of the FOR loop is:

FOR %variable IN (set) DO command [command-parameters]

Where 'variable' is a single letter, '(set)' specifies a set of one or more files, 'command' is the command-prompt command to be executed for each file in the set, and 'command-parameters' are optional parameters to be passed to 'command'.

So let's say you have been given several COM .dll files that you need to register within Windows' Registry. You could put them all in one folder and can then run the following command from within that folder:

C:\DLLs> FOR %i IN (*.dll) DO regsvr32 /s %i

What this does, for each .dll file it finds in the current directory, is to run the regsvr32 program on it. (The "/s" switch simply tells regsvr32 to do its work silently.) The variable used ("%i") is case sensitive, and it assumes the value of each .dll file in turn. The name of the .dll file is then passed to the regsvr32 program using the same "%i" variable.

A variant of the general FOR loop uses the /L switch and has this general form:

FOR /L %variable IN (start,step,end) DO command [command-parameters]

This executes a loop where %variable is first set to 'start' and is incremented by 'step' until it hits the value 'end'. (This usage should be very familiar to programmers.) So let's say you need to display the list of numbers from 1 to 5. You could do it this way:

C:\> FOR /L %i IN (1,1,5) DO @echo %i

Another variant of the FOR loop uses the /D switch and has this general form:

FOR /D %variable IN (set) DO command [command-parameters]

With this variant, if 'set' contains wildcards, then the command works on directory names instead of file names. This would be useful, for example, if you wanted to copy a set of files from a directory tree and put all the files in one directory.

For example, I have a directory called \Temp. It contains subdirectories called \Temp\Temp2 and \Temp\Barry. The \Temp\Barry and the \Temp\Temp2 directories contain files I want to copy. If I wanted to copy all of the files (without the directory structure) in the \Temp tree to a directory called \Files, I could do it like this:

C:\Temp> FOR /D %i IN (*) DO Copy %i\*.* \Files

I have left to the end what I think is the most esoteric form of the FOR loop. It seems that using the /F switch will allow you to do almost anything—if you can only figure out how to use it. The general form is:

FOR /F "options" %variable IN (`command`) DO command [command-parameters]

(Sorry it doesn't all fit on one line.) There are several 'options you can use, and the first 'command' (the one surrounded by the backwards apostrophes) can be any command-prompt command. So here's one example of how to use this:

FOR /F "Usebackq Delims==" %i IN (`dir/b c:\temp\a*.tmp`) DO @echo c:\temp\%i | Findstr /f:/ "a b c"

(Sorry it doesn't all fit on one line.) This command performs a DIR/B command on C:\Temp\A*.tmp. For each file it finds, it echoes the file name and pipes it to the Findstr command, which looks for all lines containing an "a", "b", or "c". A pretty odd thing to do, granted, but at least it shows how you can use /F. I suggest you do FOR /? to get a complete list of what all the options are, and then you're basically bound only by your imagination on figuring out how to use it.

You can also use the FOR command within a batch file. The only thing that's different is that you double the %-signs, whereas at the command line you only use one %-sign. For example, if the second example above were to be placed in a batch file, it would look like this:

FOR /L %%i IN (1,1,5) DO @echo %%i

Thursday, June 17, 2021

Seeking A but Found (Fascinating) B Part Two

Of course, because I gave up looking and clicked "Publish" for the previous post, it meant I was sentencing myself to have a solution revealed to me when I clicked a link in my search results as this page - https://stackoverflow.com/questions/51076632/exclude-certain-subfolders-when-scanning-the-contents-of-a-folder - revealed the following solution:


for /F "delims=" %%D in ('dir /B /S /A:D "C:\myFolder" ^| findstr /I /V /C:"\\Users\\" /C:"\\Windows\\"') do echo/%%D

with $RECYCLE.BIN listed in the "C:\exclude.txt" file.

for /F "delims=" %%D in ('dir /B /S /A:D "C:\myFolder" ^| findstr /I /V /L /G:"C:\exclude.txt"') do echo/%%D


Seeking A but Found (Fascinating) B

I love DOS, but I don't always understand it. For example, if I go to my I: drive and type:

Dir *.* /s

that command will include the files in my Recycle.bin.

I don't want those files to be included. Thus, I went to Google.com and entered the following search string:

In the search results, I clicked the first line and ended up learning about PowerShell: list the files in the recycle bin older 100 days. Was that what I wanted to know. No, not really. However, I liked the fact that I discovered information about a topic I know very little about - Powershell - to start my day. When I typed: Dir *.* /s at the command line, I saw this result:

And when I changed the directory to I:\$RECYCLE.BIN and entered the Dir *.* /s command, I saw this:

Thus, the actual files on my I: drive is
308495 - 5311 = 303,184
and
the actual directories on my I: drive is
283767 - 160368 = 123,399
When I started my day, all I wanted to do was to go to my I: drive and type dir i:\*.* /s and see
303,184 File(s)
123,399 Dir(s)
Maybe I'll figure out how to do that another day.

Monday, October 12, 2020

Wednesday, June 3, 2020

I do NOT understand

Me: I want to see all files that start with 1993
DOS: Okay... let me think... Here it is: 1996_11_02
Me: Uh, no. 1993
DOS: Okay... let me think... Here it is: 1996_11_02
Me: You're dumb.

Friday, April 10, 2020

Testing This Awesomeness Out Right Now

HOLY CRAP! THIS WILL BE AWESOME, if it works!

On Recursively delete empty directories in Windows, which was asked 10 years, 7 months ago, this solution was posted:

You can also use ROBOCOPY. It is very simple and can also be used to delete empty folders inside large hierarchy.

ROBOCOPY folder1 folder1 /S /MOVE

Here both source and destination are folder1, as you only need to delete empty folders, instead of moving other files to different folder. /S option is to skip copying(moving, in the above case) empty folders. It is also faster as the files are moved inside the same drive.

I'm currently testing out this idea now in a test directory structure and, thus far, it's working great. I created two files in Notepad and put each file in a separate folder. This means that when the above command is complete, if I have one folder with a single sub-folder under, each folder with a total of two files, I can add the above command to every DOS batch file I have ever created that uses "md" (make a directory) commands!

Wednesday, April 8, 2020

Thinking about File Name Analysis

I'm not sure what I will do with this information, but I wanted to do some research.
https://ss64.com/nt/dir.html
https://janakiev.com/blog/python-filesystem-analysis
https://superuser.com/questions/570760/find-filenames-with-certain-pattern-on-windows-command-line

From https://www.windows-commandline.com/find-files-based-on-modified-time:

for /F "tokens=2" %i in ('date /t') do dir /T:C | findstr /C:%i /B

This command gets the list of all the files from the current folder that are created on the same day. You do not need to specify the date, the command automatically picks the current date.

You can add /s option to dir command to search in sub directories also. However, the file names will not have full path names, they are printed folder by folder. So you can make out which folder a file belongs to.

Monday, December 16, 2019

Automation

I found this article - The Coders Programming Themselves Out of a Job - very interesting.

I use a DOS batch file at work regularly to automate what was a manual task when I started working here 1358 days (3 years, 8 months, 2 weeks, 4 days) ago. The DOS batch file creates a back up for each sub-folder in the primary folder I use to store my RoboHelp project files.

For me, my thoughts about automation are intertwined into these posts on this blog with the label DOS batch files.

Wednesday, August 21, 2019

How to Test For These Abnormalities?


Editor's Note: Updating the page that is dedicated to tracking progress of the CD to MP3 progress is the appropriate location for the information within this post. However, due to the layout decision to use a table to store tabular information and due to the screenshots below being necessary to explain the subject, it would be ugly. Thus, that page has been updated to include a reference to this post. To see the page that is dedicated to tracking progress of the CD to MP3, see CDs to MP3 Documentation.

I have not documented my progress with my music collection lately so here I go.  I ran the command "dir *.mp3 *.wav *.wma *.3gp *.amr" in the directory o:\music_shell and had 119,535 files, which is a lot of music files. However, it is a false number because of some abnormalities in my files. I found the following examples to illustrate the type of work that still needs to be done in my music collection on my external hard drive.
Notice in the red rectangle that the first file uses "Steve Miller Band" while the second file uses "The Steve Miller Band" which results in double (44) the actual files on this specific album.

Notice how the first file and the second file have the same file size, are both "Track 1" - one of those files needs to be renamed to include the actual track information and the other file needs to be deleted.

Notice how even though these are the files in the "50_Spindle" folder, there are files that have "1051_Spindle" in the file name. Files that have "1051_Spindle" in the file name need to be verified to be the actual same tune as the file that has "1050_Spindle" in the file name and then the files that have "1051_Spindle" in the file name need to be deleted from this folder.

The files that begin "Amen" in the Purple rectangles need to be compared to the files that begin with the track number and the duplicates then need to be deleted.

All that said, I ran other commands:
  • "dir /s" in the directory o:\music_shell and had 122569 files
  • "dir /s /ah" in the directory o:\music_shell and had 4913 files. The number of hidden files are not included in the above total. A quick review of that list of hidden files indicate that these are the file types which exist in the o:\music_shell folder:
    • JPG files
    • DB files
    • INI files

Wednesday, August 14, 2019

Makes Zero Sense!

See these commands?
This command
"Move 2010*.* 2010"
should move any file that begins with "2010" to the "2010" folder. Instead, files that begin with "2014" and with "2015" were moved to the "2010" folder.

WHY?!?!?

This command

"Move 2011*.* 2011"
should move any file that begins with "2011" to the "2011" folder. Instead, files that begin with "2013" and with "2014" were moved to the "2010" folder, plus 1 file that begins with 2011.


WHY?!?!?
WHY?!?!?
WHY?!?!?
WHY?!?!?
WHY?!?!?
WHY?!?!?

Figuring out "WHY?!?!?" is my current task at work.

Wednesday, May 22, 2019

Hello DOS Resource!

I use DOS commands daily at work. I know, I know, I should learn PowerShell or Python or some other scripting language, but I know what I know, which is DOS. Until Windows is rebuilt without it, I'll use it. Thus, I'm always on the lookout for opportunities to learn more than I know now about DOS. That's why, on Displaying the Command Prompt Window, I posted this comment:

I've noticed that on other posts, there are URLs that are listed to other sites that provide additional details about the page. Since DOS commands are so vast and there are so many sites that are about DOS commands, I wonder if there could be a list of URLs that have been helpful to the author.

A response to that comment has now introduced me to this site - http://ss64.com - a brand-new (to me) website!

Wednesday, April 3, 2019

Holy CRAP!! I FEEL DUMB!!

It started with this query on 3/29/2019:

Can I use Regular Expressions to replace this:
|
<title>CTE/Voice Services/Grandstream Patient Room Phones - External Resources</title>

With this:

<title>CTE - Voice Services - Grandstream Patient Room Phones - External Resources</title>
|

Since that innocent query, the amount of knowledge I've been trying to process is staggering. Every time I think about learning how to code by taking a Kirkwood class or through Lynda.com, I read something like this link on stack overflow that Chris M sent to me in his effort to help me solve this and shake my head. So much of that page goes sailing over my head! I think, “You’re a dumb English major that didn’t know what he was doing in 1994 and typed “c:\del *.*” in DOS (when I really wanted to just delete .txt files, but I didn’t know I could type “C:\del *.txt”.

Then I think about how I *know* now (today) that “c:\del *.*” was a dumb thing to type and that I have learned so much about DOS since then, by reading sites like stack overflow, by experimenting, by asking questions of Chris, Brian C, and anyone else I come in contact with via email or the Internet.

That brings me back to wanting to use that gnawing feeling of “I’m really stupid and could never learn this stuff” as motivation to do just that: learn it! Time will tell if I learn as much about regular expressions as I have learned about DOS!

I went back to earlier in the process of trying to figure this out because I thought I had seen where the command (see, I don’t even know if I call the things I’m trying to create “commands” or “expressions” or something else!) would look inside the <title> tag. That led me back to https://regexr.com/4bdf0 and guess what?

It seems to work for me. I ran it over my directory and it was working like I expected. I then replaced the <title> & </title> tags with <h1> & </h1> because I have the same type of thing within the Heading 1 tags. Preliminary results are encouraging!

I’m lucky in that I can regenerate these HTML pages any time I want and they are stored within a single directory (c:\rh\drdoc\e-r) which means if I screw something over, I simply regenerate the files and don’t paint myself into a corner by “permanently” mucking up my files.

So, crossing fingers, I think I have it!

Friday, January 4, 2019

Doing Dashing DOS ... Commands

I needed a more efficient way to copy "S:\Disaster Recovery Planning\*.*" to "C:\RH\DRM\DRExternal2" by using Xcopy commands. How to say no to all “do you want to overwrite” prompts in a batch file copy?, which seems fairly popular, according to the page's "asked / viewed / active" stats:


From that page, I was able to write this command:

xcopy "s:\disaster recovery planning" c:\rh\drm\drexternal2 /e /d /y


I'm happy with the testing I've done thus far. I modified "S:\Disaster Recovery Planning\Statement-Rejection\Statement-Rejection.docx" and then re-ran the command. As you can see below, it copied the modified file:


I did learn something today, which is a really good thing. Thanks to the web page above, I learned my user profile does not have the Manage Auditing user right, per this error message:


As a concluding note, I located this page - https://social.technet.microsoft.com/Forums/ie/en-US/c4486f3d-45cb-4bfd-a6ae-f4a797d5b4ee/robocopy-error-you-do-not-have-the-manage-auditing-user-right?forum=winservergen - through Google. The proposed solution on the page You need to have the command prompt to be run as administrator. right click run as admin  does not work for me as my admin account doesn't have access to network drives - it was granted to me so that I could install software locally. I understand this because if my admin user account did have access to network drives, it would be a lot of authority that I really do not want!

Monday, October 29, 2018

ROBOCOPY Returns to My Focus

I started out the day running this DOS Batch file that uses the more efficient "ROBOCOPY" command instead of the tested / verified / awesome "XCOPY" command. However, I quickly realized that the commands below were insufficient because each ROBOCOPY command was set up to copy the entire "S:\Disaster Recovery Planning" folder into each of the sub-folders within the c:\rh\drm folder structure, which is not what I want to do. What I want is:

to copy the CSV files within S:\Disaster Recovery Planning to the c:\rh\drm\csv folder
to copy the DOC* files within S:\Disaster Recovery Planning to the c:\rh\drm\DOC folder
repeating that logic for each of the file types I've specified in the commands below.

This is my original code:

cd\
c:
cd c:\rh\drm
rd /s /q c:\rh\drm\csv
rd /s /q c:\rh\drm\doc
rd /s /q c:\rh\drm\jpg
rd /s /q c:\rh\drm\png
rd /s /q c:\rh\drm\pdf
rd /s /q c:\rh\drm\ppt
rd /s /q c:\rh\drm\txt
rd /s /q c:\rh\drm\vsd
rd /s /q c:\rh\drm\xls
rd /s /q c:\rh\drm\zip
pause
md c:\rh\drm\csv
md c:\rh\drm\doc
md c:\rh\drm\jpg
md c:\rh\drm\png
md c:\rh\drm\pdf
md c:\rh\drm\ppt
md c:\rh\drm\txt
md c:\rh\drm\vsd
md c:\rh\drm\xls
md c:\rh\drm\zip
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\csv *.csv /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\doc *.doc* /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\jpg *.jpg /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\png *.png /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\pdf *.pdf /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\ppt *.ppt* /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\txt *.txt /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\vsd *.vsd* /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\xls *.xls* /E
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\zip *.zip /E
pause

I am very happy I had inserted "pause" commands so that I could check the progress in the destination folder because I quickly realized my syntax was wrong. Thanks to https://stackoverflow.com/questions/472692/how-to-copy-a-directory-structure-but-only-include-certain-files-using-windows, I know what I would need to do and, actually, I've improved upon the process. My original idea was to use a separate command to copy each file type to a folder for that file type, but now I realize, I can use a single command to copy the files to a single directory structure, use SearchMyFiles.exe to look for all the files in that single folder structure - instead of specifying each sub-folder - select all the files, cut, and paste into the root folder. Then I still use a second DOS Batch file to move each file type to a folder for that file type.

cd\
c:
cd c:\rh\drm
rd /s /q c:\rh\drm\csv
rd /s /q c:\rh\drm\doc
rd /s /q c:\rh\drm\jpg
rd /s /q c:\rh\drm\msg
rd /s /q c:\rh\drm\png
rd /s /q c:\rh\drm\pdf
rd /s /q c:\rh\drm\ppt
rd /s /q c:\rh\drm\txt
rd /s /q c:\rh\drm\vsd
rd /s /q c:\rh\drm\xls
rd /s /q c:\rh\drm\zip
pause
md c:\rh\drm\csv
md c:\rh\drm\doc
md c:\rh\drm\jpg
md c:\rh\drm\msg
md c:\rh\drm\png
md c:\rh\drm\pdf
md c:\rh\drm\ppt
md c:\rh\drm\txt
md c:\rh\drm\vsd
md c:\rh\drm\xls
md c:\rh\drm\zip
pause
robocopy "S:\Disaster Recovery Planning" c:\rh\drm\baggage *.csv *.doc* *.jpg *.msg *.png *.pdf *.ppt* *.txt *.vsd* *.xls *.zip /E

One command instead of 10 is always a cool thing!
It makes the requirements for using SearchMyFiles.exe easier as well. Here's the before:



See? Much more slick!

Wednesday, October 17, 2018

Housekeeping On The G EHD

Yes, this was taken prior to deleting 104063 empty directories on the G: EHD. How... What.... Why....

Let me answer.

Here is the contents of a DOS batch file I have used to create a directory for each day of each year starting with 1998 and going through December 2018. I also have code to create a bucket for all files earlier than 1998 for the 1970s, the 1980s, and 1990 - 1997. The code is long and would cause a tremendous amount of scrolling, but I want it available if I need to refer to the version of it that I revised today, as I was writing this post. Thus, I created a page to store it, which is here.