Script to generate checksums for new files only? - windows

I’ve created (well, lifted) a script for powershell to monitor a folder for new files, when new files are added it runs the following batch file to generate checksums:
for %%a in (*.txt) do md5sum "%%a" >> "%%a.md5"
Files are regularly being added to the folder so I only want the script to run on the new files. At the moment it runs from scratch each time generating checksums for the same file over and over. I tried an IF NOT EXIST statement but this only checks the folder for a single instant:
if not exist "*.txt.md5" (for %%a in (*.txt) do md5sum "%%a" >> "%%a.md5")
I presume I need to scan the folder, generate a list of files and then skip over the ones that have checksums? This is where it goes a bit over my head - can anyone advise?
One thing to note - it’s important each file has a separate checksum (e.g. File.txt - File.txt.md5) opposed to generated in a single list, as the files go their separate ways.
Thank you!

Related

Run batch script on all files in a folder

I have a folder receiving 'calculations' files daily. Sometimes a few files arrive. I want to execute a batch script on all of them in a loop. Files may have different name but may contain three extensions only: .ta, .tb, .tc.
I am trying to run a batch script on all of them, rename the files so that they contain a timestamp and move to archive folder. I have tried multiple combinations of the below but I can't figure it out:
cd C:\test\files
FOR %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
In the above case, combine.bat is not executed properly and the file is not moved. However, when I run it as follows it is launching combine.bat and moving the file. But if I have two files, combine.bat is executed on both but the second one is not moved to archive folder:
FOR /R C:\test\files %%x IN (*.ta, *.tb, *.tca) DO (
call C:\test\batches\combine.bat
move %%x "C:\test\archive\calculations.txt-%date:/=-%_%time::=-%"
)
The problem is that there are other folders under C:\test\files and I want the 'combine.bat' to run for this specific path only and not for any sub-folders. Is it failing because I have changed directory in the beginning to C:\test\files and the loop is then calling to another directory?
Appreciate you help.

Need a BAT file to copy & rename all files in specific tree

I need to create a .bat that runs through a multilayered directory... copying certain files that contain the following suffix: '.full.jpg' to save as '.jpg'
What I've tried:
copy /y "C:\Users\myname\Desktop\maindir\*.full.jpg" "C:\Users\myname\Desktop\maindir\*.jpg"
However, I cannot get it to work.
The .bat is located in the 'maindir' directory and ran from the terminal (cmd).
Here's an example scenario that maps closely to mine:
Existing Files:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
Example Output Wanted:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture1.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.jpg
I'd appreciate any help towards this as I haven't been able to do it yet. I will run across a directory structure whereby the top level directory will contain 15+ directories and each containing 20+ directories with 100+ files in each lowest directory.
Thanks.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\Users\myname\Desktop\maindir"
FOR /r "%sourcedir%" %%a IN (*.full.jpg) DO (
FOR %%b IN ("%%~dpna") DO ECHO(COPY "%%a" "%%~dpnb.jpg"
)
GOTO :EOF
The inner for examines the drive-path-name only of the complete filename in %%a (ie. it drops the .jpg) and delivers the drive-path-name of that name (ie. drops the .full) to which you add .jpg and job done.
You would need to change the setting of sourcedir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files.

How to use a batch file to delete a line of text in a bunch of text files?

I have a bunch of txt files in my D drive which are placed randomly in different locations. Some files also contain symbols. I want a batch file so that I can delete their specific lines completely at the same time without doing it one by one for each file and please refer to a code which does not create a new text file at some other location with the changes being incorporated i.e. I do not want the input.txt and output.txt thing. I just need the original files to be replaced with the changes as soon as I click the batch file.
e.g
D:\abc\1.txt
D:\xyz\2.txt etc
I want both of their 3rd lines erased completely with a single click and the new file must be saved with the same name in the same location i.e. the new changed text files must replace the old text files with their respective lines removed. Maybe some sort of *.txt thing i.e i should be able to change all the files with the .txt extensions in a drive via a single batch file perhaps in another drive,not placing my batch file into each and every folder separately and then running them. Alternatively a vbs file is also welcomed.
This uses a helper batch file called findrepl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file below.
It will search for every *.txt file on drive d: and remove line 3.
#echo off
for /r "d:\" %%a in (*.txt) do (
echo processing "%%a"
type "%%a"|findrepl /v /o:3:3 >"%%a.tmp"
move "%%a.tmp" "%%a" >nul
)
pause

Combining two FOR commands (/f /l) in a batch file

Okay guys, I am fairly new to working with batch files and I have two files I have previously created which are both working independantly.
I am looking to combine them but I still do not fully understand the FOR command. I was hoping someone could combine these two sets of code into one and if possible explain how the came up with the code they used from my two sources.
This file copies another file (in this case test.txt) to every subdirectory in a directory
FOR /R d:\ %%A IN (test.txt) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%A
FOR /R h:\ %%A IN (test.txt) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%A
This file copies and renames another file X amount of times (in this case 5) renaming each succesive copy in increments of 1.
For /l %%1 in (1,1,10) do (
copy test.txt test%%1.txt > nul
)
Basically I want the selected file (test.txt) to be copied from a set location to every subdirectory within a directory and then copied in each folder X amount of times and renamed with increasing values e.g.
test1.txt
test2.txt
test3.txt
etc.
Thankyou in advance.
This is actually very straight forward.
You already have functioning code that copies from the source to each subdirectory. In pseudo code: FOR (each directory) DO COPY source to target.
You also have code that can copy the file 10 times with incrementing names. You want to do this for each directory in the 1st step. So, again in pseudo code, it will look something like this:
FOR (each directory) DO (
COPY source to target
FOR (N=1 TO 10) DO COPY source to targetN
)
None of the syntax above is real, except that the parentheses after the DO are actually how you allow a batch FOR command to execute a block of multiple commands. (Actually there are other techniques to do this, but the parens work just fine.)
The part that you are missing is how to insert the incrementing number into the %%A target name. This is done by using FOR variable modifiers, as described at the end of the FOR documentation that you can access from the command line by typing HELP FOR, or FOR /?.
The modifiers allow you to deconstruct a file specification into its component parts. Note that the file doesn't have to physically exist, the file spec can still be broken down into the constituent parts.
%%~dpnA = drive:\path\baseName (no extension)
%%~xA = .extension, including the dot.
You've already got the incrementing number - I'm going to use %%N instead of %%1. So the full target will be the concatenation of the 3 components: %%~dpnA%%N%%~xA.
Putting it all together gives the full solution:
FOR /R d:\ %%A IN (test.txt) DO (
copy d:\%username%\Desktop\Test\Resources\test.txt %%A
FOR /L %%N IN (1 1 10) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%~dpnA%%N%%~xA
)

batch file to merge .js files from subfolders into one combined file

I'm struggling to get this to work. Plenty of examples on the web, but they all do something just slightly different to what I'm aiming to do, and every time I think I can solve it, I get hit by an error that means nothing to me.
After giving up on the JSLint.VS plugin, I'm attempting to create a batch file that I can call from a Visual Studio build event, or perhaps from cruise control, which will generate JSLint warnings for a project. The final goal is to get a combined js file that I can pass to jslint, using:
cscript jslint.js < tmp.js
which would validate that my scripts are ready to be combined into one file for use in a js minifier, or output a bunch of errors using standard output.
but the js files that would make up tmp.js are likely to be in multiple subfolders in the project, e.g:
D:\_projects\trunk\web\projectname\js\somefile.debug.js
D:\_projects\trunk\web\projectname\js\jquery\plugins\jquery.plugin.js
The ideal solution would be to be able to call a batch file along the lines of:
jslint.bat %ProjectPath%
and this would then combine all the js files within the project into one temp js file. This way I would have flexibility in which project was being passed to the batch file.
I've been trying to make this work with copy, xcopy, type, and echo, and using a for do loop, with dir /s etc, to make it do what I want, but whatever I try I get an error.
You could create a batch file with the following contents:
#echo off
pushd "%~1"
for /r %%x in (*.js) do (
type "%%~x"
)
popd
and then run it via:
jslint.bat PATH > tmp.js
If you don't want to use redirection, you can try:
#echo off
pushd "%~1"
echo.>tmp.js
for /r %%x in (*.js) do (
copy tmp.js + "%%~x" tmp.js > NUL
)
popd
note that for simplicity, I haven't bothered doing any error-checking (e.g. checking whether an argument is supplied (although if one isn't, it'll just use the current directory), testing that tmp.js doesn't already exist, etc.).
A great place for tips on batch files is DosTips.com
Have a look at http://nefariousdesigns.co.uk/archive/2010/02/website-builds-using-make/
The post is written for Linux world but still you might be able to salvage something out of it.

Resources