Assign `call system()` result to a Fortran variable - fortran

I am using this piece of code to review the number of files of a folder
call system ('dir c:\data /B /A-d|find /v /c "*"')
I need to assign it to a variable. I have tried everything.

Related

Modify Robocopy script to read number appended to files in folder, add one, and rename file that is being copied

So I have a very simple script set up on the task scheduler to move screenshots once a day from one folder to another. Thats working fine, but when the screenshots are taken, they are automatically give a name by Steam, e.g. "Screenshot.png", "Screenshot 1.png", "Screenshot 2.png" etc depending on how many are in the folder. So when the files are emptied in to the other folder each day, every set of screenshots taken on that day start again at "Screenshot.png" and go up from there. The result is that when they are moved, they are going to be either overwriting or appending (1) or something (not sure which as haven't had this running yet!)
So I want to change the following batch file to find the highest number in "Screenshot xxx.png" in the destination folder, add one, rename the file in the source folder to this number and then do the move. This will have to fit in a loop so it can go through all files in the source folder.
#echo off
set X=1
set "source=C:\Users\Leigh\AppData\Local\Colossal Order\Cities_Skylines\Screenshots"
set "destination=D:\Leigh\Pictures\SkyLinesScreenshots"
robocopy "%source%" "%destination%" /mov
exit /b
I had a play and found a script to determine which the next number should be, but couldnt get the regex right:
#echo off
setlocal enableextensions disabledelayedexpansion
setlocal enabledelayedexpansion
set "nextFile=0" & for /f "delims=" %%a in ('
findstr /b /r /x
/c:"\bScreenshot\b\s[0-9]+\.png"
') do if %%~na geq !nextFile! set /a "nextFile=%%~na+1"
endlocal & set "nextFile=%nextFile%"
echo Next file will be: %nextFile%
If I get that right I will integrate them together and work out the problem. This is pretty clear what needs doing, so could anyone either give me the correct regex, or even more if they so wish.
Thanks in Advance!
Windows 10 64-bit. PowerShell 5
Archive screenshots to timestamp folder (mmddyy) using CMD and robocopy. Delete archives older than 14 days with PowerShell.
#echo off
setlocal enableextensions
set "source=C:\Users\Leigh\AppData\Local\Colossal Order\Cities_Skylines\Screenshots"
set "destination=D:\Leigh\Pictures\SkyLinesScreenshots\%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4,2%"
robocopy "%source%" "%destination%" /mov
rem delete archives older than 14 days. Remove -whatif
powershell -command "get-childitem "D:\Leigh\Pictures\SkyLinesScreenshots" |? {$_.psiscontainer -and $_.lastwritetime -lt (get-date).adddays(-14)} |% {remove-item $_ -recurse -force -whatif}"
exit /b
You can adjust the age threshold through .adddays. This is in WhatIf mode. Remove -whatif to delete the folders.
Windows 10 64-bit
Count, increment, move w/ rename with Windows 10 64-bit CMD, for, set, and move.
If the source screenshots are created without enough interval this will happen:
Remove the echo when it is working.
CMD:
#rem Count, increment, move w/ rename with Windows 10 64-bit CMD, for, set, and move
cmd /q /v /e
pushd "%HOME%\Pictures\SkyLinesScreenshots"
FOR /f %g in ('dir /a-d ^| find "File(s)"') do set z=%g
cd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
FOR /f "delims=" %g in ('DIR /b /a-d') do (
set /a z+=1 > nul
echo move "%g" "%HOME%\Pictures\SkyLinesScreenshots\screenshot!z!.png"
)
popd
exit /b
Script:
#rem Count, increment, move w/ rename with Windows 10 64-bit CMD, for, set, and move
#echo off
setlocal enableextensions
pushd "%HOME%\Pictures\SkyLinesScreenshots"
FOR /f %%g in ('dir /a-d ^| find "File(s)"') do set z=%%g
cd "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Screenshots"
FOR /f "delims=" %%g in ('DIR /b /a-d') do (
set zname=%%g
Call :increment
)
popd
exit /b
:increment
set /a z+=1 > nul
echo move "%zname%" "%HOME%\Pictures\SkyLinesScreenshots\screenshot%z%.png"
exit /b
Results:

Renaming a list of files #1 by another list of files #2

Renaming a list of files (in one folder) contained in a text file like MyList1.txt by another list of files contained in another text file like MyList2.txt.
I would like to use a Windows command batch file (not PowerShell, script, etc.) which renames a list of files in one folder, contained in a text file, by another list of files contained in another text file.
Suppose I have a list of files inside a text file. These files are in one folder, for example D:\Librarian
File D:\Test\MyList1.txt contains:
Directory.zip
Subject.zip
Penny.zip
Car.zip
Auto.zip
One_Upon_A_time.zip
All is relative.zip
Zen of graphics programming – Corilolis.zip
PC Magazine – July 1999 No.22.zip
Bytes is over with quantum.zip
And I want to replace them by the file names listed in D:\Test\MyList2.txt containing:
Patatoes.zip
Chips.zip
Hot Dogs Sweet.zip
Ice Cream_Very – Good.zip
We must eat to live.zip
Ketchup on potatoes is a muxt.zip
The Magazine are owned by few guys.zip
Desert are not necessary_in life.zip
Sugar_is_a_legal_drug.zip
People-who_don’t-like_pizzas_don’t like bread.zip
So
Directory.zip will become Patatoes.zip
Subject.zip will become Chips.zip
Penny.zip will become Hot Dogs Sweet.zip
etc.
Both MyList1.txt and MyList2.txt have same number of lines (same number of file names).
OS is: Windows 10.
Must use batch (cmd)
One folder - D:\Librarian have files to be renamed.
Another folder have the two files MyList1.txt and MyList2.txt
MyList1.txt contains the list of files from D:\Librarian
MyList2.txt contains the new name of files for D:\Librarian
------Begin 1: dbenham----------------------------------------------------------
I have tried what you wrote and it's working except for 2 file names.
Probably Windows command interpreter doesn't accept some punctuation in these file names.
Here what appear on command prompt (DOS under Windows 10):
(I remove #echo off of course to be able to see that.)
D:\>setlocal disableDelayedExpansion
D:\>pushd "d:\Librarian"
d:\Librarian>(for /F "usebackq eol=: delims=" %F in ("d:\Test\MyList1.txt") do (
set "from=%F"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
) ) 0 (<) d:\Test\MyList2.txt
It's not (<) but only < but for x reason 0< is not visible.
d:\Librarian>(
set "from=Directory.zip "
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
d:\Librarian>(
set "from=Subject.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
...
d:\Librarian>(
set "from=Zen of graphics programming Corilolis.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
The system cannot find the file specified.
d:\Librarian>(
set "from=PC Magazine July 1999 No.22.zip""
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
The system cannot find the file specified.
d:\Librarian>(
set "from=Bytes is over with quantum.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
Windows command interpreter doesn't like - character probably in the following file names:
PC Magazine - July 1999 No.22.zip
Zen of graphics programming - Corilolis.zip
But since a lot of my files have the hyphen character, I would like an advice to get the batch file working also for such file names.
Later... After to use CMD to rename all files (inside MyList1.txt and MyList2.txt and in the folder D:\Librarian) all work good.
I think I copy and paste files from this page and so not same characters for all even if - can appear to be - like you say they can be different on console or on web page.
I can't say I understand all what you did (so bright)!
For example why do:
pushd "d:\Librarian"<br>
(<)d:\Test\MyList2.txt
(No () around the <)
I find Windows commands are just extraordinary. Well more to see what people, like you, can do so with so few commands. Just unbelievable!
------End 1: dbenham----------------------------------------------------------
Hope to not bore you ...
Can I do the same if my files are in folder and subfolders?
Suppose my files are distributed in the following folders:
D:\Librarian\
D:\Librarian\S1\
D:\Librarian\Plane\
MyList1.txt
D:\Librarian\Directory.zip
D:\Librarian\Subject.zip
D:\Librarian\Penny.zip
D:\Librarian\S1\Car.zip
D:\Librarian\S1\Auto.zip
D:\Librarian\S1\One_Upon_A_time.zip
D:\Librarian\S1\All is relative.zip
D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip
D:\Librarian\Plane\PC Magazine - July 1999 No.22.zip
D:\Librarian\Plane\Bytes is over with quantum.zip
MyList2.txt
D:\Librarian\Patatoes.zip
D:\Librarian\Chips.zip
D:\Librarian\Hot Dogs Sweet.zip
D:\Librarian\S1\Ice Cream_Very - Good.zip
D:\Librarian\S1\We must eat to live.zip
D:\Librarian\S1\Ketchup on potatoes is a must.zip
D:\Librarian\S1\The Magazine are owned by few guys.zip
D:\Librarian\Plane\Desert are not necessary_in life.zip
D:\Librarian\Plane\Sugar_is_a_legal_drug.zip
D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip
What I would like is for example:
ren "D:\Librarian\Directory.zip" "D:\Librarian\Patatoes.zip"
ren "D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip" "D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip"
Renaming a file is trivial, and reading from a single file is simple with FOR /F. The only tricky bit is reading the corresponding line from a second file, which is the same issue when merging two text files. The most efficient solution is to redirect stdin to the 2nd file for an entire block of code. You can then use SET /P to read successive lines
The following script will generally work, but note that no attempt is made to detect or handle name collisions that could result from a rename operation.
#echo off
setloacal disableDelayedExpansion
pushd "d:\Librarian"
<d:\Test\MyList2.txt (
for /f "usebackq eol=: delims=" %%F in ("d:\Test\MyList1.txt") do (
set "from=%%F"
setlocal enableDelayedExpansion
set "to=" %= Clear any existing value =%
set /p "to=" %= Read the next line =%
if defined to ren "!from!" "!to!"
endlocal
)
)

Batch Remove all files without extensions from list

I am making a simple program to list all the files in a certain folder and I want to remove all files (from the text file or wherever is more efficient) that don't have extensions.
This is my current code:
dir /s/b *.* > E:/Project.sav
for /f "Delims=" %%A in (E:/Project.sav) do (
echo %%A
)
and I get a mix of files like this:
C:\Users\User\Desktop\TEst\05f429069e65dwqewqeqweqeq0a3f294cc3128ab
C:\Users\User\Desktop\TEst\063bf0fui3ucinyh3ruincuiwhwu3gssssybwyrbweib
C:\Users\User\Desktop\TEst\hs_err_pid3080.log
C:\Users\User\Desktop\TEst\hs_err_pid5220.log
If you don't need the intermediate file, then all you need is the DIR command:
dir /s /b *.
Note that the above will include directory names as well as file names, as does your original code. If you only want files, then add the /A-D option:
dir /s /b /a-d *.
If you need the complete listing in E:/Project.sav, then you can use FINDSTR with a regular expression to filter out the entries that contain an extension:
findstr /vr "\.[^\\]*$" "E:/Project.sav"
if "%%~xA" neq "" echo %%A
should do what you want.
If "the extension of the filename in %%A" is not equal to "" echo....

Batch Script List Checking

I'm working on a Windows 7 command line batch script to automate our build process. Some of the projects that need to be built are dependent on other projects. The normal process (assuming no dependencies) is to do a dir /AD /B from the repository root directory to get the names of the projects to compile. (This works with the folder structure and naming convention we are using.) What I'd like to do is add a file, buildorder.txt, to specify any necessary build order for dependencies only, which may or may not include all of the projects that need to be built.
Here is some pseudo code to demonstrate what I'm trying to accomplish:
if exist buildorder.txt (
for /F %%i in buildorder.txt do (
rem build each project in order specified in buildorder.txt
)
)
for /F %%i in ('dir /AD /B') do (
rem build all of the remaining project that have not already been built
)
Keep in mind, some (or perhaps all of the) items discovered for the second for loop will have already been processed in the first for loop, and I would rather not build those projects a second time.
Any thoughts on how this can be accomplished would be much appreciated! Thanks!
For the second for /F loop, do
findstr /i "%%i" buildorder.txt >NUL || ( rem build %%i )
The conditional execution evaluates false and proceeds with the build if %%i is not found in buildorder.txt.

Copy list of file names from multiple directories

i'm new to the forum. I need some advice. I need help with a code to take a text file with a list of file names and copy the files from multiple folders in to one folder, but it has to end up with the most recent modified date of the file. There are hundreds of folders containing the thousands of files.
I have a batch code below to copy the files, but it does not always end up with the newest file. If there is some way to make the code below search the folders in numerical order, that would work instead of it searching in a random order. I do not care if the code i end up using is batch, vbs, or whatever.
mkdir %userprofile%\desktop\print
set FIILELIST=%userprofile%\desktop\print.txt
set FILESPATH="\\server\folder"
set DESTPATH=%userprofile%\desktop\print
for /f %%X in (%FIILELIST%) do call :COPY_FILES "%%X"
goto :eof
:COPY_FILES
for /r %FILESPATH% %%I in (%~1) do copy "%%I" "%DESTPATH%"
for /r %FILESPATH% %%I in (%~1) do Xcopy /D "%%I" "%DESTPATH%"
XCOPY /D will only copy if the destination file does not exist or if the destination file is older than the source.