Certutil with Batch Script - batch-file

Trying to encode all my *.pdf and *docx within a folder and its subdirectories, to base64. So far, since the certutil specifies "from source" "to destination" or an overwrite with -f switch, am clueless on how to get this working, even with a FOR loop. Thanks for your help.
Thanks putu. Corrections taken. Well, am trying to overwrite the existing documents to its base64 encoding which I've been successful in doing one document at a time. The challenge is in automating the process into all documents in folders and sub-folders. I've tried using the the "for" statement:
for /R %%a in (*.docx, *.pdf) do certutil -f -encode %%a %%a.
This was just a test as certutil don't have any syntax for encoding multiple files at once.

The /R function of the for loop returns the full file spec including the drive and path. I suspect either the path or file name contains one or more spaces so they need to be quoted. Use the following:
for /R %%a in (*.docx, *.pdf) do certutil -f -encode "%%a" "%%a"

Related

Having some specific images file and need to copy selected files to another folder

I am new to the coding here,
I was googling out several threads here regarding my requirement, tried but not getting succeeded.
My basic requirement is.
I have a folder having 1000 images.
Need to copy specific images and transfer to some other folder.
This is the basic basic requiremnet
Open to any way
Either Xcopy or robocopy or powershell
Tried with Xcopy
xcopy /d /y /s "\Your Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
Here I am getting issues that One file is getting copied fine but not I need to copy several files . How will modify on that script
Tried with robocopy
robocopy "source path" "destination path" "file name"
same issue , how can I transfer multiple files.
Say I have listed down the names in excel or notepad.
Tried with PowerShell
Get-Content e:\np.txt | ForEach-Object {copy-item $_ E:\day1\sorted}
Here, The NP.txt - is the list of files in that notepad file.
Now the destination is E:\day1\sorted
But I get confused where will be the source path, Where should be the source path, should I write
to read your list, use a for /f loop:
for /f "delims=" %%a in (np.txt) do echo %%a
Instead of echo %%a (for testing), just insert your copy command:
for /f "delims=" %%a in (np.txt) do (
xcopy /d /y /s "\Your Image Folder\%%a" "%userprofile%\Desktop\Master Image Folder\"
)
(you may have to adapt it a little bit depending on the exact content of np.txt. Also, I'm not sure, why you use /s - shouldn't be necessary according to your description)

How to filter on exact file extension?

I have .las and .lasx files which have the same file name in a DATA folder. I am trying to apply processes on the *.las files only. But it seems that the wildcard pattern *.las also includes *.lasx files:
for %%f in ("%DATA_PATH%\*.las") do
or
lasground -i "%DATA_PATH%\*.las" -merged
The parameter before -merged is supposed to be the list of my *.las files only, but during my tests it always includes the *.lasx files.
Any idea about how to get all my .las files without getting the .lasx ones?
After providing the documentation and the actual program you are using I would give these two examples a try.
Make a variable with all the file names.
#ECHO OFF
for %%G in ("%DATA_PATH%\*.las") do (
IF /I "%%~xG"==".las" call set list=%%list%% "%%G"
)
lasground -i %list% -merged
Make a list of files and use the -lof option.
#ECHO OFF
(for %%G in ("%DATA_PATH%\*.las") do (
IF /I "%%~xG"==".las" echo %%G
)
)>List.txt
lasground -lof List.txt -merged
The Where command is a simpler way to filter this directly, i.e. output only the extension specified:
#Where "%DATA_PATH%":*.las 2>Nul >"LasOnly.txt"
You can then use "LasOnly.txt" as input to lasground using the -lof option as already advised:
#lasground -lof "LasOnly.txt" -merged

Delete semi duplicate files

I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).

certutil -hashfile : processing multiple files in a drag-dropped folder

I'm trying to create a batch script that runs certutil -hashfile MD5 on each file in a folder and write the output to a file.
I have this code below except it only works on the files in the current folder,
I would like it to work such that when a folder is drag-dropped into the batch file .bat it processes that folder only.
for %%a in (*) do certutil -hashfile %%a MD5 >> MD5_log.txt
Also is there a way to get it to output spaces in the log file between iterations of the certutil output text?
It is actually very simple!
Simply change (*) to ("%~1\*") or other command-line arguments. If you have multiple drag-drop folders, do "%~1\*" "%~2\*", etc. Using quotes (") can prevent issue with space. So paths are now quoted. And %%a becomes %%~a, which means to de-quote.
Alternatively, you can set a variable containing all paths and process them one by one.
Result:
for %%a in ("%1\*") do certutil -hashfile "%%~a" MD5 >> MD5_log.txt
Store following file as .bat file and change testfolder and outputfile as desired.
#ECHO OFF
setlocal enabledelayedexpansion
:: Set the variables for this script.
set testfolder=C:\Test\
set outputfile=md5_files.txt
cd %testfolder%
echo > %outputfile%
for %%f in (".\*.*") do (
certutil -hashfile %%f SHA1 >>%outputfile%
echo %%ff
)
PAUSE

Batch script to remove parts of a filename

Help please!
I need to remove time stamp and file number off of the filename using a windows batch script. Thank you! Thank you in advance!
OLD Filename= CAM168-NOTSET.2013.10.01.18.45.45.882.jpg
NEW Filename= CAM168-NOTSET.jpg
#ECHO OFF
SETLOCAL
SET "fname=CAM168-NOTSET.2013.10.01.18.45.45.882.jpg"
FOR %%i IN ("%fname%") DO FOR /f "delims=." %%j IN ("%%i") DO ECHO REN "%%~i" "%%~j%%~xi"
GOTO :EOF
New name merely ECHOed.
If by "remove time stamp and file number", you mean remove everything between the first and last dot, preserving the extension, then you don't need a batch script. A single one liner will do.
This command will rename all .jpg files in the current directory. It will support final names up to length 50, excluding date, number, and extension. Add additional ? if you have longer names, or you can remove some ? if your names are shorter.
for %F in (*.jpg) do #ren "%F" "??????????????????????????????????????????????????%~xF"
Double up the percents if you use the command in a batch file.
You can put any file mask, or list of files, etc in the in () clause.
If you are just going to rename .jpg files, then you can simply use:
ren *.jpg ??????????????????????????????????????????????????.jpg
Refer to How does the Windows RENAME command interpret wildcards? for info on why this works.
Lloyd has an interesting solution that will only rename files that match a template of
name.number.[number.]...ext, expressed as regex. But it requires installation of a third party executable. The same specificity can be achieved easily with pure script using a hybrid JScript/batch utility called REPL.BAT.
Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH, then the following will match all files that match the regex template:
for /f "eol=* delims=* tokens=1,2" %A in (`dir /b /a-d^|repl "(.*?)(\.\d+)+(\.\w+) a" "$&*$1$3"') do ren "%A" "%B"
Double up the percents if used within a batch script.
using renamer (cross-platform file renamer) with these input files:
CAM168-NOTSET.2013.10.01.18.45.45.882.jpg
CAM169-NOTSET.2013.10.01.18.45.45.883.gif
CAM170-NOTSET.2013.10.01.18.45.45.883.jpeg
and this command:
$ renamer --find '/(.*?)(\.\d+)+(\.\w+)/' --replace '$1$3' *
produces these filenames:
CAM168-NOTSET.jpg
CAM169-NOTSET.gif
CAM170-NOTSET.jpeg

Resources