Loop over files in directory with if statement (batch file) - for-loop

I'm trying to create a bat file to compile all of the .java files in a directory and to run the class called Main (and output to tulemused.txt) I was able to do this for a single .java file with a set name but I don't know how to use batch scripting well enough to loop over all of the files.
#ECHO on
FOR /d %%f DO IF NOT %%f=="compile.bat" javac -encoding utf-8 %%f
java -Dfile.encoding=UTF8 Main > tulemused.txt
#ECHO off
This is how far I got

Try this:
#ECHO on
FOR /r %%f In (*) DO IF NOT "%%~nf"=="compile.bat" javac -encoding utf-8 "%%~f"
java -Dfile.encoding=UTF8 Main >tulemused.txt

Found that this works even better since java wants to compile all of the classes at once but credit to Wasif for fixing the original loop
#ECHO on
set string=javac -encoding utf-8
FOR /r %%f In (*.java) DO call :concat %%~nf
%string%
set /P name="What is the name of the class with the main method? "
java -Dfile.encoding=UTF8 %name% >result.txt
pause
:concat
set string=%string% %1.java

Related

How to run a command in Windows batch script?

I'm trying to feed the file name dynamically to a command through windows batch script. Here's my code,
#echo off
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml %%I)
pause
Basically, I want to get all the .java files from any given directory and feed those java files to another command which will run for each file. In place of %%I I am trying to provide the java file dynamically. But it says java command not recognized.
If I use java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml path/filename.java alone in a batch file. It works fine.
I have tried the following also,
#echo off
set i=-1
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (
set /a i=!i!+1
set filenames[!i!]=%%I
)
set lastindex=!i!
for /L %%f in (0,1,!lastindex!) do (
java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml !names[%%f]!
)
pause
But again it says java command not recognized.
What am doing wrong here? Why I can not pass the .java file names dynamically?
Your named your variable with project path "path", and it's bad, because as soon as you do
set /p path="Enter the project path: "
you overwrite the system "path" and then Windows cannot find your "java.exe".
Rename it to project_path:
set /p project_path="Enter the project path: "
for /R "%project_path%" %%I in .......

Get FCIV to scan hidden files within a batch script

I am attempting to hash all or most of the files off of a machine using a batch script. What I thought would be straight forward was of course not as FCIV will not scan hidden files. I attempted to make a for loop that would scan the individual files themselves but what works in the command line does not work in the batch file.
I would go to the root of my drive and attempt this:
FCIV -r -both c:\
However I noticed that quite a few files were missing (even as admin) with most of them being hidden files.
Thanks,
Any help would be appreciated.
I use this snippet of the code for CertUtil, it might also work for FCIV
cd /d %targetDir%
for /r %%e in (*) do (
if exist "\\?\%%e" (
FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "\\?\%%e" %hashType% ^| findstr /v "certutil hash"`) DO (SET var=%%F)
echo !var! "%%e" >> %hashDatabaseOutput%
SET /A fileCount += 1
) else (
echo ERROR 1 - Failed to Hash file, File or Directory contains special characters >> %errorlog%
echo %%e >> %errorlog%
echo.>> %errorlog%
)
)
cd /d "%workingDir%"
I have a similar issue as you, I'm scanning some files in C:\ but those aren't hidden files.
EDIT: Seems that using this method makes no difference because even when the file path that is feed to FCIV, the hashing will fail because it cannot find the file. you can look at my question here https://stackoverflow.com/questions/62111957/fciv-fails-to-find-some-files-when-hashing-c-drive
EDIT2: Found the root cause, it's caused by redirection when you attempting to scan files in C:\Windows\System32. One guy explained it clearly here
The system cannot find the path specified
Since FCIV only have 32bit mode, the only solution is to use other hashing program like rHash with 64bit.

Batch Script to generate multiple command-line instructions

I am writing a batch file Primary.bat which collects all the text file names from a particular directory and pipe the output to Generator.bat
The contents of Primary.bat currently:
#echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
Rem Following command will write the names of all files in a text file
dir /b "C:\InputOutput\SourceFiles" > "C:\InputOutput\Generator.bat"
So, the contents of Generator.bat will be:
input1.txt
input2.txt.
input3.txt
unitedstates1.txt
unitedkingdomsales.txt
majorregion100.txt
Now I need to add code in Primary.bat so that all the above lines gets created in Generator.bat with some additional text as per below:
converter.java C:\InputOutput\SourceFiles\input1.txt C:\InputOutput\OutFiles\input1.rtg
converter.java C:\InputOutput\SourceFiles\input2.txt C:\InputOutput\OutFiles\input2.rtg
converter.java C:\InputOutput\SourceFiles\input3.txt C:\InputOutput\OutFiles\input3.rtg
converter.java C:\InputOutput\SourceFiles\unitedstates1.txt C:\InputOutput\OutFiles\unitedstates1.rtg
converter.java C:\InputOutput\SourceFiles\unitedkingdomsales.txt C:\InputOutput\OutFiles\unitedkingdomsales.rtg
converter.java C:\InputOutput\SourceFiles\majorregion100.txt C:\InputOutput\OutFiles\majorregion100.rtg
Once Primary.bat ran you should then just be able to double-click Generator.bat which will execute all of the commands.
Thanks in advance
There are still some points I don't understand about your code but as far as I understood your task, this should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TYPE NUL>Generator.bat
ECHO #ECHO OFF>>Generator.bat
FOR /F %%F IN ('DIR /B InputOutput\SourceFiles') DO (
ECHO START converter.java "C:\InputOutput\SourceFiles\%%F" "C:\InputOutput\OutFiles\%%~nF.rtg">>Generator.bat
)
As you only want to execute converter.java, you also can skip START. I've suggested START to be able to decide how you want to execute your application (eg. by adding /b or /p or /w or whatever as a parameter for START).
The following should work. Since converter.java is a source file, it can't be executed. I therefore assume you mean java Converter, which will work assuming you have written a class called Converter, placed it in Converter.java, and compiled it with javac Converter.java to create Converter.class.
#echo off
>C:\InputOutput\Generator.bat (for %%f in (C:\InputOutput\SourceFiles\*.txt) do (
echo java Converter "%%f" "C:\InputOutput\OutFiles\%%~nf.rtg"
))
NOTE!! This is assuming that all of the files in the Directory has an .txt extension as per your example
#echo off
if exist "C:\InputOutput\Generator.bat" move /Y "C:\InputOutput\Generator.bat" "C:\InputOutput\Generator.back"
echo #echo off > "C:\InputOutput\Generator.bat"
setlocal enabledelayedexpansion
for /F %%a in ('dir /b C:\InputOutput\OutFiles\') do (
set result=%%a
set renamed=!result:.txt=.rtg!
echo converter.java "C:\InputOutput\SourceFiles\!result!" "C:\InputOutput\OutFiles\!renamed!" >> "C:\InputOutput\Generator.bat"
)
endlocal

Read a Folder's subfolders and files in these subfolders using Windows cmd

I am working on a batch script which reads a file and "find and replace" this character "╡" to "µ"
So far I was able to write following code:
#echo off
Setlocal enabledelayedexpansion
chcp 65001
Set "Pattern=╡"
Set "Replace=µ"
For %%# in ("E:\CopiedVault\*") Do (
Set "File=%%~nx#"
Ren "%%#" "!File:%Pattern%=%Replace%!"
)
The problem I am facing is how I don't know how this script can be modified ,by which it can read subfolders of folder "CopiedVault", and then can find and replace files and their names.
This script works perfect if it is able to find the file.
Try
dir /s /b E:\CopiedVault
inside a forloop and for each occurance rename the file.

Java Version in a batch file

My question, is extremely similar to the following SO question: How to get Java Version from batch script?
In fact, it did almost solve my problem. The only difference is that I've to check the Java version based on %JAVA_HOME%, which the user is free to modify. The issue that I'm facing is with this code:
#ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
IF "%JAVA_HOME%"=="" (
#ECHO Please set JAVA_HOME environment variable
EXIT /B
)
#echo "%JAVA_HOME%"
REM Checking JAVA_VERSION
SET JAVA_VERSION=
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%"\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
SET "JAVA_VERSION=%%g"
)
%JAVA_HOME%% in my system points to "C:\Program Files\jdk1.7.0_25" (notice the space in the path)
Even with the quotes, I get the following error in command line:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Any idea as to how to solve this problem? (The comments to the aforementioned article also mentions this issue). I'm working on a Windows 7 machine
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1 ^| findstr /i "version"') do (
SET "JAVA_VERSION=%%g"
)
Edit the %JAVA_HOME% Variable into:
C:\"Program Files"\jdk1.7.0_25
if you want it automated, type
set %JAVA_HOME%=C:\"Program Files"\jdk1.7.0_25
REASON WHY:
The batch file does not accept the quotes; It is identifying them as a single file. So it attempts to find "C:\Program Files\jdk1.7.0_25" NOT as a folder path, but as a folder NAME in your root folder. If you type in
C:\"Program Files"\jdk1.7.0.25 it identifies that "Program Files" is a single file. If there are no redirection operators, It would think that the path would be like this;
C:\Program\Files\jdk1.7.0_25. It worked for me; It should probably work for you.
Hope that helped
-SonorousTwo
I had a similar problem, take a look at my QA: How to get Java version in a batch script subroutine?
From my answer:
It seems that piping the output to findstr was stripping the quotes for some reason.
I managed to fix the problem without Epic_Tonic's workaround (which would be very difficult to do with a path as a parameter).
This should work in your case:
set first=1
for /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1') do (
if !first!==1 echo %%~g
set first=0
)
Note: first is for only searching the first line of java -version's output (reference).

Resources