Copy filename to file with same number - filenames

So this is a bit vague to describe so I'll use a picture:
I have around 150 DWG files that have the same content as the SVG's (they're both vector drawing formats converted 1 to 1). I'd like to apply the same filename from the DWG's to the SVG's that start with the same number.
So I end up with:
001_TERMINAL.dwg
001_TERMINAL.svg
002_DIFFUSER.dwg
002_DIFFUSER.svg
etcetera...
I'm using a PC with Windows 10.
How can I implement a solution to my problem?
Thanks!

Assuming it's always 3 digits in the *.svg file names:
set DIR=C:\mydir
#rem Allow repeated setting of !variables! in the FOR loop below
setlocal enabledelayedexpansion
for %%I in (%DIR%\*.dwg) do (
#rem "~n" to pick out just the filename part of the %%I variable
set BASENAME=%%~nI
#rem Substring - batch file style
set PREFIX=!BASENAME:~0,3!
echo !PREFIX! ... !BASENAME!
rename !PREFIX!.svg !BASENAME!.svg
)
Note this will need to be in a batch file for the %%I to work.
The main complication there is using variables in a multi-line FOR loop.
For these you have to use the delayed expansion option, to enable the variable to be expanded each time round, rather than when the line is parsed. This means you have to use !variable! instead of the more normal %variable% in a batch file.

Because you are on Windows, PowerShell is a great candidate to solve this.
For the script below, the length of the numeric part in front of the underscore character doesn't matter, as long as there is an underscore in the .dwg filename, as visible in your question.
Just replace 'c:\folder' here below with the path your files are stored in.
$folderPath = "c:\folder"
$files = Get-ChildItem ([System.IO.Path]::Combine($folderPath, "?*_*.dwg"))
for ($i=0; $i -lt $files.Count; $i++)
{
$file = $files[$i]
$dwgFileName = $file.BaseName
$index = $dwgFileName.IndexOf("_")
$numberPart = $dwgFileName.Substring(0, $index)
$svgFilePath = [System.IO.Path]::Combine($folderPath, "$numberPart.svg")
if ([System.IO.File]::Exists($svgFilePath))
{
Rename-Item -Path $svgFilePath -NewName "$dwgFileName.svg"
}
}

Using bash:
#!/bin/bash
for f in *.dwg; do
IFS='_' read -r -a arr <<< "$f"
mv ${arr[0]}.svg ${f%.*}.svg
done

Related

Saving a batch variable in a text file

I am trying to save a batch variable into a text file. I currently have this code:
#echo off
Set var=6
#echo %var%>txt.txt
For /f "tokens*" %%i in (txt.txt) do #echo %%i
Pause
It's supposed to save the 6 into the variable var and then write the variable in a text file. I want to do this to save user input into a text file so that when the batch program is terminated it will hold the variables.
There is a little problem with redirection. You are redirecting a "stream"; they are numbered 0-9. 0 is for "Standard Input" (STDIN), 1 is for "Standard Output" (STDOUT), 2 is for "Error Output" (STDERR).
If you use the redirection symbol > without a stream number, it defaults to "1".
So echo text>txt.txt is just an abreviation for echo text 1>txt.txt
Now it's getting tricky: echo 6>txt.txt won't echo "6" to the file, but tries to redirect "Stream 6" (which is empty) to the file. The Standard Output echo is off goes to the screen, because "Stream1" is not redirected.
Solution:
If you try to redirect a number or a string which ends with a number, just use a different syntax:
>txt.txt echo 6
Use the set command to get the contents of a file:
set /p var=<filename
Use the echo command to put into a file:
#echo Contents Of File > "FileName"
To append another line to the end of the file, use:
#echo Contents Of File >> "FileName"
Also, put the commands on separate lines or use '&&' between them on the same line.

powershell replace text with

Im still fairly new to powershell and i'm still trying to learn.
Here is my issue im working with:
I have a bunch of bat files which i need to replace with a certain text.
Only Catch is that there is one thing in the text which needs to be replaced with a variable.
Let me illustrate:
Printer1.bat
Printer2.bat
text that need to be written:
#echo off
setlocal EnableDelayedExpansion
::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8)
do (
if "!Version!" equ "this" (
set Version=Windows %%a
) else if "!ver: %%a=!" neq "%ver%" (
set Version=this
)
)
if "!Version!" equ "Windows XP" (
rundll32 printui.dll,PrintUIEntry /ga /n\\printserver\$printername
call P:\Script\wait.cmd 1
sc stop spooler
call P:\Script\wait.cmd 4
sc start spooler
call P:\Script\wait.cmd 1
) else echo Scriptet skal ikke kjøres på Windows7
Ping –n 5 –w 1 127.0.0.1>null
"#
If you notice the variable $printername which is actually the same name as the files name.
So what i want is to overwrite the file with the text but replace the variable with the filename (without .bat)
Not sure if you understand what im trying to do.
i know how to get only the filename without .bat by doing split-path and replace with ""
Ive tried so many Methods like foreach statements. .do while.. nothing i do is correct
If what you are looking for is to get the filename, without the path or file extension, you can use the [System.IO.Path]::GetFileNameWithoutExtension method for this. So something like the following would do the trick:
$batfiles = ls *.bat
foreach ($file in $batfiles)
{
$printername = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$newFileContents = #"
[... script content removed for brevity ...]
rundll32 printui.dll,PrintUIEntry /ga /n\\printserver\$printername
[... script content removed for brevity ...]
"#
$newFileContents | Set-Content $file.FullName -Encoding UTF8
}

Get a filename bat

I am new to BAT writing.
I am trying to write a batch file which will check to see if a file is above or below a certain size and then send an email accordingly. I have written something that can do this with a static file name
#echo off
setlocal
set file="ssoff.bat"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is ^< %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize less than" -body testbody
) ELSE (
echo.File is ^>= %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize greater than" -body testbody
)
In this case, the filename being checked is ssoff.bat. However, I need to have the bat check a filename which changes daily according to the date. The mask for the filename uses the date string IE: deployEAR_restartWAS_03132013.log ; deployEAR_restartWAS_03142013.log ... and so on - with the last 8 chars reflecting the date generated. The bat needs to check the latest file in that directory. For today it would be deployEAR_restartWAS_03152013.log
This logfile would not be in the parent dir either.
I thought maybe of having the bat copying over the latest file from that dir to the parent dir and then checking its size? Or using the static part of the filename 'deployEAR_restartWAS_' and then passing the last part of it through a date variable?
But I'm not sure what approach would be best, and I'm sure there are others that I have not thought of.
The bat file run time would be the same day as the date variable at the end of the filename.
for /f %%i in ('dir /b /a-d /od deployEAR_restartWAS_*.log') do set name=%%i&set size=%%~zi
echo latest file is %name% size %size%
Can't work out in which directory this set of .log files resides from your description. You tell us it won't be in the parent directory and your only reference appears to be that directory.
If the .log files are not in the CURRENT directory, all you need do is add the directory name to the deploy... thus: Q:\wherever\it\maybe\deployEAR_restartWAS_*.log or, if the file's path contains spaces, quote the name thus: "Q:\where ever\it\may be\deployEAR_restartWAS_*.log"

Create a file containing a string and no CR or LF in a batch script

I am trying to generate sha256 hash of a string in a batch script.
For this purpose I found sha256sum.exe (link)
this application accepts file arguments only.
Thus I need to create a file with the string, and no trailing space nor CR or LF.
in my batch script I did:
echo.|set /P =%var%>temp.txt
sha256sum temp.txt > temp2.txt
I used the first line to remove CR & LF however in temp.txt file I see a single trailing space char, which is then used by sha256sum.exe.
I am trying to get rid of that trailing space character at the end of the first line of the text file.
I am a linux user and I wasn't expecting such a simple thing to be a problem.
Thanks in advance.
Simplest thing would be to replace all whitespace;
set h=0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC
echo."%h%"
set h=%h: =%
echo."%h%"
>> "0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC "
>> "0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC"
Edit
Sorry, I didnt realise the extra space was in the written value, how about
#echo off
set var=hashme
for %%a in (%var%) do (
echo/|set /p ="%%a"
)>temp.txt
FOR /F "tokens=1" %%i in ('sha1sum temp.txt') do SET var=%%i
echo "%var:~0,-1%"
I downloaded that sha1sum.exe and for "hashme" the output is correct;
"fb78992e561929a6967d5328f49413fa99048d06"
You can do:
set /p =%var%>temp.txt <NUL
instead of:
echo.|set /P =%var%>temp.txt

capturing CMD batch file parameter list; write to file for later processing

I have written a batch file that is launched as a post processing utility by a program. The batch file reads ~24 parameters supplied by the calling program, stores them into variables, and then writes them to various text files.
Since the max input variable in CMD is %9, it's necessary to use the 'shift' command to repeatedly read and store these individually to named variables. Because the program outputs several similar batch files, the result is opening several CMD windows sequentially, assigning variables and writing data files. This ties up the calling program for too long.
It occurs to me that I could free up the calling program much faster if maybe there's a way to write a very simple batch file that can write all the command parameters to a text file, where I can process them later. Basically, just grab the parameter list, write it and done.
Q: Is there some way to treat an entire series of parameter data as one big text string and write it to one big variable... and then echo the whole big thing to one text file? Then later read the string into %n variables when there's no program waiting to resume?
Parameter list is something like 25 - 30 words, less than 200 characters.
Sample parameter list:
"First Name" "Lastname" "123 Steet Name Way" "Cityname" ST 12345 1004968 06/01/2010 "Firstname+Lastname" 101738 "On Account" 20.67 xy-1z 1 8.95 3.00 1.39 0 0 239 8.95
Items in quotes are processed as string variables. List is space delimited.
Any suggestions?
echo %* 1>args.txt
%* references all arguments: %1 %2 %3...
It also works with subroutines.
call :test 1 2 3
goto :eof
:test
echo 1: %1
echo 2: %2
echo 3: %3
echo *: %*
exit /b
output:
1: 1
2: 2
3: 3
*: 1 2 3
See the following website for more information:
http://ss64.com/nt/syntax-args.html
Interesting Post. It sparked my interest.
I too am needing something that could accept parameters and although this probably isn't useful to you now I thought it might be useful at some later date.
My solution is less simple - because there just isn't an elegant way to do it.
Basically, in this example the "-" can be used to identify a parameter, and the next space is assumed to be set to a value.
Legal Stuff:
So this is all my code and I don't really care how or where you choose to use it. No need to cite me it's just an example anyway.
Like this:
Microsoft Batch:Begin Copy below and save as filename.bat
#ECHO OFF
REM USAGE: this-batch-name.bat -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
SET __CURRENT_WORKING_DIRECTORY__=%~dp1
ECHO.__CURRENT_WORKING_DIRECTORY__=%__CURRENT_WORKING_DIRECTORY__%
REM # Clear Previous Variables
SET PACKAGING_BUILD_NUMBER=
SET PACKAGING_JOB_NAME=
SET GO_DEEPER=
SET RUN_COMMAND=
REM ## In order to read variables set while in a "FOR" loop
REM ## you have to set the 'ENABLEDELAYEDEXPANSION' with 'SETLOCAL'.
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
REM ## Capture Command line parameters here with a %*
FOR %%A IN (%*) DO (
REM ## If we found something with a '-' in previous pass run GO_DEEPER will be defined and thus set to the command line argument.
IF DEFINED GO_DEEPER (
REM ## When ENABLEDELAYEDEXPANSION is Set with setlocal command you have to use exclamation: i.e. '^!'
IF /I "-BUILD"=="!GO_DEEPER!" SET PACKAGING_BUILD_NUMBER=%%A
IF /I "-JOB"=="!GO_DEEPER!" SET PACKAGING_JOB_NAME=%%A
IF /I "-RUN"=="!GO_DEEPER!" SET RUN_COMMAND=%%A
SET SET GO_DEEPER=
)
IF /I "%%A" GEQ "-" (
REM ## Wow we found your command line argument that started with a '-' so set the GO_DEEPER Var
SET GO_DEEPER=%%A
) ELSE (
SET SET GO_DEEPER=
)
)
REM ## Time to grab the variables set while in delayed expansion mode
ENDLOCAL && SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER% && SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME% && SET RUN_COMMAND=%RUN_COMMAND%
REM ## Sucks, but you have to clear the '"' and "'" if it exists.
IF DEFINED RUN_COMMAND (
SET RUN_COMMAND=%RUN_COMMAND:"=%
SET RUN_COMMAND=%RUN_COMMAND:'=%
)
IF DEFINED PACKAGING_JOB_NAME (
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:"=%
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:'=%
)
IF DEFINED PACKAGING_BUILD_NUMBER (
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:"=%
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:'=%
)
REM ## Now we can try to run the command function if the -run was used...
IF DEFINED RUN_COMMAND (
CALL:--%RUN_COMMAND% "'%PACKAGING_JOB_NAME%'","'%PACKAGING_BUILD_NUMBER%'"
) ELSE (
ECHO Try running:
ECHO %0 -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
)
GOTO DONE
:--RUN_FUNCTION
ECHO running... %~0
SET VARPASSED1=%~1
SET VARPASSED2=%~2
IF DEFINED VARPASSED1 ECHO VARPASSED1 was %VARPASSED1%
IF DEFINED VARPASSED2 ECHO VARPASSED2 was %VARPASSED2%
ECHO Add your code to process here...
GOTO:EOF
:DONE
ECHO We got the following results...
IF DEFINED PACKAGING_JOB_NAME ECHO PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME%
IF DEFINED PACKAGING_BUILD_NUMBER ECHO PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER%
IF DEFINED RUN_COMMAND ECHO RUN_COMMAND=%RUN_COMMAND%
</pre> </code>
Microsoft Batch END Copy
RESULTS:
__CURRENT_WORKING_DIRECTORY__=C:\dev\a\win\sysprep\
running... :--RUN_FUNCTION
VARPASSED1 was "'Running...'"
VARPASSED2 was "'This...'"
We got the following results...
PACKAGING_JOB_NAME="Running This Job"
PACKAGING_BUILD_NUMBER="1.2.3 build 405"
RUN_COMMAND=RUN_FUNCTION