Script that generates commands and run them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pelomero
Posts: 7
Joined: 01 May 2020 03:08

Script that generates commands and run them

#1 Post by pelomero » 01 May 2020 03:25

Hello

Summay of my question:

I want to generate commands and run them based on the files I find in the current folder and subfolders; the selection of the files need to be based on their extension.

Example:

On
D:\Folder 1
D:\Folder 1\SubFolder
D:\Folder 1\SubFolder\SubSubFolder


I have lots of files. I want to run a script on "D:\Folder 1" and based on that script, I want to select all files with the extension *.p7s and *.p7m from the root folder and subfolders. And based on the selected files, I want to run this command:
  • openssl smime -inform DER -verify -noverify -in pathOfFileSelected -out pathOfFileSelectedWithoutTheExtension
So if I found file doc.pdf.p7s on D:\Folder 1, the command would be:
  • openssl smime -inform DER -verify -noverify -in "D:\Folder 1\doc.pdf.p7s" -out "D:\Folder 1\doc.pdf"
I would love to do this in cmd without recreating using a programming language.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script that generates commands and run them

#2 Post by penpen » 01 May 2020 05:45

This might help you (untested; remove the "echo(" if that matches your expectation):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /r "D:\Folder 1" %%a in ("*.p7s" "*.p7m") do echo(openssl smime -inform DER -verify -noverify -in "%%~fa" -out "%%~dpna"
goto :eof
penpen

pelomero
Posts: 7
Joined: 01 May 2020 03:08

Re: Script that generates commands and run them

#3 Post by pelomero » 01 May 2020 06:00

Yes, it's working. Thank you VERY MUCH.

pelomero
Posts: 7
Joined: 01 May 2020 03:08

Re: Script that generates commands and run them

#4 Post by pelomero » 10 May 2020 00:35

Hello to this wonderful community!

Three more questions. (2 related to this topic, the third kinda of :D )

I hope it's okay I didn't open another topic.

1. I saw that in my first question, in order to remove the extension, penpen wrote "%%~dpna" .
What do I need to write to add the string "-i" characters to the name of the file, without removing the extensions.

So let's say I have the file "D:\folder\file.pdf". I want to apply script below which creates the file "D:\folder\file-i.pdf"

Code: Select all

@echo off
for /r "%cd%" %%a in ("*.pdf" ) do (
magick -density 150 "%%~fa" -compress JPEG [b]"??????"[/b])
goto :eof

2. I want to extract all zip files found in a folder and extract them with Powershell to a folders.

Code: Select all

for /r "%cd%" %%a in ("*.zip") do powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; 
[IO.Compression.ZipFile]::ExtractToDirectory('%%~fa', '00. name of zip file'); }
How can I can I add the string "00." to the name of the zip file to create that specific folder. I've tried lots variations, but I can't find the solution.

3. I have a script which takes all word files (*.doc *docx) from a folder and converts them in PDF files.

I want to call this code from command prompt, without manually entering in PowerShell, like I did in the first question.
Once again, I've tried lots of variations, I just receive errors.

Code: Select all

$path = "c:\test\1" #Target directory for converting Word files
$word_app = New-Object -ComObject Word.Application

#Convert .doc and .docx to .pdf
Get-ChildItem -Path $path -Filter *.doc? | ForEach-Object {
    $document = $word_app.Documents.Open($_.FullName)
    $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
    $document.SaveAs([ref] $pdf_filename, [ref] 17)
    $document.Close()
}
$word_app.Quit()

P.S. Like I said previous, I hope it's okay I didn't open another topic.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script that generates commands and run them

#5 Post by penpen » 12 May 2020 04:06

Ad 1)
You could simply remove the extension, add "-i" and then add the extension only (for more details see "for /?"); siimple example:

Code: Select all

for %%a in ("*.pdf") do echo("%%~dpna-i%%~xa"
Ad 2)
I'm not sure if you are looking for something like that (or do you want to do that from within powershell?):

Code: Select all

for /r "%cd%" %%a in ("*.zip") do powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; 
[IO.Compression.ZipFile]::ExtractToDirectory('%%~fa', '00.%%~na'); }
Ad 3)
If i remember right, you must set the execution policy to accept your source file, before you could execute that file.
I think there were 4 policies (could be more, or i might error on the exact spelling, you might want to look that up):
- Restricted: No script is allowed to run.
- AllSigned: Only signed scripts that aren't downloaded can be run.
- RemoteSigned: Only signed scripts can be run.
- Unrestricted: All scripts can be run.

So i think the following could be used to execute "sample.ps1" with some sample parameters:

Code: Select all

powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File "sample.ps1" "some" "params"
Ad P.S.)
I think the first two aditional questions are related close enough to this topic, but the third question should have used an own topic. But i'm sure that topic already exists here somewhere, so if someone searches for it he could find that already, so i don't think that this is an issue this time, but you should keep that in mind for additional questions.


penpen

pelomero
Posts: 7
Joined: 01 May 2020 03:08

Re: Script that generates commands and run them

#6 Post by pelomero » 12 May 2020 04:55

Thank you, thank you.

Regarding the full path name and the name of the file, I've found some info regarding on how should I manipulate them, but I didn't manage to sort it out.

I've got reference from here:
https://stackoverflow.com/questions/659 ... h-with-cmd
https://stackoverflow.com/questions/925 ... h-in-batch
and another stack overflow topic which the solution was taken from here, but no luck.


Once again, thank you.

Post Reply