I have been able to successfully run the below code on an individual file, but I would like to find a way to have it run so that it loops through all the files shown in a directory. I believe that loops are possible within CMD.exe, but I am not having any success. Any help would be greatly appreciated.
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete
If you have any ideas for how I might be able to achieve this it be a huge help. I am working to build this as part of a larger workflow and make it repeatable.
UPDATE
I was able to iterate(Loop) through all of the files in my directory by creating a batch file and placing it within the directory that I am going to be using for converting moving forward
for /r %%i in (*.csv) do "C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete %%i
Note that I used a for Loop to achieve what I wanted.
for /r %%i in ('list directory if command not being run in specific directory already; you can also but * for all files in directory or *.ext for the extension of only certain files) do 'add in command %%i
Option 1: You can actually convert multiple documents with just one execution of Open/Libre Office.. without a loop. Just use a wildcard, eg:
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF mydir\*.csv
Option 2: I assume that Windows has a limit on the number of files you can pass in that way.. not sure what that limit is.. but if you exceed that limit, you can use this python program I wrote to do this just a few days ago.. as part of a project I am still working on. In my case I am converting .doc to .odt, and obviously my directories aren't the same as yours. But it would be quite easy for you to adjust this to your needs... One thing I wanted to do with mine is to check for the existence of the converted file and skip it if it exists.. so that if a few new files are added it can be run again to convert them without redoing them all...
The docpath should be set to the location of your files, and soffice to the pathname of your Calc exe.. and if you leave things the way I have them, that's where the program puts the converted files too.. and checks for their existence. Also, I walk the path looking for input files.. so if for whatever reason you have your files in different subdirectories, it will find them and put the conversion in the same directory.
import os, sys, re, subprocess
docpath = 'doc'
soffice = 'C:\Program Files\LibreOffice 5\program\soffice.exe'
convert = '"%s" --headless --convert-to odt --outdir "%s" "%s"'
def plog(fmt = '', *args):
sys.stdout.write(fmt % args)
sys.stdout.flush()
def log(fmt = '', *args):
plog((fmt + '\n'), *args)
def convert():
odtfiles = []
for subdir, dirs, files in os.walk(docpath):
for file in files:
if not file.endswith('.doc'):
continue
docfile = os.path.join(subdir, file)
odtfile = re.sub(r"\.doc$", ".odt", docfile)
plog("%s -> %s: " % (docfile, odtfile))
if not os.path.isfile(odtfile):
plog('Converting.. ')
subprocess.check_output(convert % (soffice, docpath, docfile), shell=True)
log('OK.')
odtfiles.append(odtfile)
return odtfiles
odtfiles = convert()
Just install Python27.. and set the convert string to have the correct settings for your conversion. Along with a few other changes that should be pretty easy.. but if you need help, ask in comments.
Related
i want to create a portable roblox but these bothers me
i am not so good at batch file but how to i run these exe under these directory i mean
if roblox has an update the "version-3bxxxxxxxx" will change and i cannot run the exe anymore
start "" "C:\Program Files (x86)\Roblox\Versions\version-3b33190189084158\RobloxPlayerLauncher.exe" -app
start "" "C:\Program Files (x86)\Roblox\Versions\version-3b33190189084158\RobloxPlayerLauncher.exe" -app
i try to search on the internet give nothing more =(
I hope you found the help you needed by now, sad no one responded after all this time. This is not as elegant, but I had the same issue and I did this as my solution:
cd\Progra~2\Roblox\Versions\version-* <-just need a "*" wildcard there
start RobloxPlayerLauncher.exe -app
Hope this helps others that want to run a batch file for programs like Discord and Roblox etc. as their directory names always change with updates.... The practice is annoying and I wish devs would consider end users setups.
rEd2k's answer is fine for use directly on the command line. With a batch script, you can automate it.
Search for the exact folder name with a for /d loop and use the found folder as working folder with the start command:
for /d %%a in ("C:\Program Files (x86)\Roblox\Versions\version-*") do set "rootdir=%%a"
start /d "%rootdir%" RobloxPlayerLauncher.exe -app
NOTE: if there is more than one matching folder (versions), rootdir will contain the last found folder (by string sorting on NTFS), which is probably good enough.
I am running a Jenkins project which runs under Windows. I have a dir to find a file (html file) from my directory. This shows up
reports\All Smoke_2018-01-23T084148.270-0600\TestLog.html
I know the first directory will always be "reports" and the file name will be TestLog.html. However, the middle directory (All Smoke_2018-01-23T084148.270-0600) will depend on the date and time. So what I want to do is
echo "some string" >> reports\*\TestLog.html
it will actually be more complicated than that. But it does not appear to allow me to use an * as a directory name (as Unix would).
Is there another way to refer to the file? I know find has /s which is how I find it and findstr also has a /b. But how can I get the file name to echo to it?
I have a parent folder that contains multiple folders within it. Then, each of these nested folders contains 4 files that make up a GIS shapefile and have different extensions (i.e., ".dbf", ".prj", ".shp", and ."shx"). I am new to coding (outside of R) and do not know whether this can be automated with Python or if I need to run a shell script (I'm working on a windows). I have very rudimentary coding schools so documentation would be great (and/or suggestions of "dummy" sites to read).
Here is an example of the current file structure (showing the four files I want to rename with the subfolder name):
Parent Folder: "Raptors"
Subfolder: "Falco_peregrinus"
File 1: "ra03310.dbf"
File 2: "ra03310.prj"
File 3: "ra03310.shp"
File 4: "ra03310.shx"
Here is what I would like the four files to be renamed to:
File 1: Falco_peregrinus.dbf
File 2: Falco_peregrinus.prj
File 3: Falco_peregrinus.shp
File 4: Falco_peregrinus.shx
Thanks.
For a batch file solution
#echo off
for /d %%a in ("c:\...\Raptors\*") do ren "%%~fa\*.*" "%%~na.*"
For each folder inside the parent one, rename all the files inside the folder to the name of the folder but keeping the extension
for command is used to iterate over the list of folders (/d) under the parent folder. For each of the folders, the replaceable parameter %%a will hold a reference to the subfolder and the code in the do clause is executed for each one.
The code in the do clause executes a ren command, for all the files under the subfolder (%%~fa is the folder being processed with full path), changing its name to the name of the folder (%%~na).
edited The answer is not completely correct. While the basic idea of using only one ren command to rename all the files under each folder is probably the fastest way, the way ren command handles wildcards makes this code fail if the folder name contains dots. To be sure the code will not fail, it is necessary to iterate over the files, renaming each one
for /d %%a in ("c:\...\Raptors\*") do for %%b in ("%%~fa\*") do ren "%%~fb" "%%~nxa%%~xb"
For each folder (%%a), for each file inside the folder (%%b), rename the file to the name of full folder name (%%~nxa) with the extension of the file (%%~xb)
You could use almost any programming language (probably including R) to do this. Python is a good choice here because it has such friendly syntax.
A extremely simple script that will solve your problem might look like this
import os
import os.path
'''
Given a file name, returns a pair with the name and extension (hello.txt => [hello,txt])
'''
def split_name(file_name):
return file_name.rsplit('.',1)
'''
Recursively renames files in subdirectories of base_directory so each file is named the subdirectory name plus the extension
WARNING! You will be very sad if you have multiple files with the same extension in any of those folders
def rename_file(base_directory):
#Get the folder name for base_directory (c:\users\foobar => foobar)
directory_name = os.basename(base_directory)
#List the files in base_directory
for path in os.listdir(base_directory):
old_name = base_directory + '/' + path
#If the path points to a file, rename it directory name + '.' + extension
if os.path.isfile(old_name):
new_name = base_directory + '/' + directory_name + '.' + split_name(path)[1]
if not os.path.exists(new_name):
os.rename(old_name,new_name)
else:
print("ERROR:"+new_name+" exists")
else:
#If it's a subfolder, recursively call rename files on that directory.
rename_files(old_name)
Also, I stongly suggest Learn Python The Hard Way by Zed Shaw and Dive Into Python by Mark Pilgrim
a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:
for /d %a in (*) do #for %b in (%a\*) do #ren %a\%b %a%~xb
let's analyze it
the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.
This can be done with batch only, I publish this script only to demonstrate how easy this is in Ruby
# enumerate all subfolders of raptors
Dir.glob("raptors/**/*/") do |folder|
# remember the prefix
pre = File.basename(folder)[/.+_/]
# enumerate all files under this folder
Dir.glob("#{folder}*.*") do |file|
File.rename(file, "#{File.dirname(file)}/#{pre}#{File.basename(file)}")
end
end
There is another answer with python code, this code changed my GIS file names based on folder name very well:
Thanks #Martin Evans.
I came across a way to convert my .bat with dependencies on tool to an .exe file. However when I try using the script and run the .exe created, I always getting an error. Seems I modified the script incorrectly.
Anyone can help, please?
Here's the code with my modifications:
#ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
REM Usage:
MakeExeFromBat BatFileToConvert -bat MyProgram.bat
REM
REM Required Parameters:
BatFileToConvert -save MyProgram
REM Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
IncludeFile -include Tool.exe
REM Additional files to include in the Exe file.
REM You can include external tools used by the batch file so they are available on the executing machine.
SETLOCAL
REM Configuration (no quotes needed):
SET PathTo7Zip=C:\Desktop\
REM ---- Do not modify anything below this line ----
SET OutputFile="%~n1.exe"
SET SourceFiles="%TEMP%\MakeEXE_files.txt"
SET Config="%TEMP%\MakeEXE_config.txt"
SET Source7ZFile="%Temp%\MakeEXE.7z"
REM Remove existing files
IF EXIST %OutputFile% DEL %OutputFile%
REM Build source archive
ECHO "%~dpnx1" > %SourceFiles%
:AddInclude
IF {%2}=={} GOTO EndInclude
ECHO "%~dpnx2" >> %SourceFiles%
SHIFT /2
GOTO AddInclude
:EndInclude
"%PathTo7Zip%\7za.exe" a %Source7ZFile% #%SourceFiles%
REM Build config file
ECHO ;!#Install#!UTF-8! > %Config%
ECHO RunProgram="%~nx1" >> %Config%
ECHO ;!#InstallEnd#! >> %Config%
REM Build EXE
COPY /B "%PathTo7Zip%\7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%
REM Clean up
IF EXIST %SourceFiles% DEL %SourceFiles%
IF EXIST %Config% DEL %Config%
IF EXIST %Source7ZFile% DEL %Source7ZFile%
ENDLOCAL
This doesn't really convert a bat file to an exe. It just creates a selfextracting archive (exe) which contains the bat file. On execution it extracts the file to a temporary folder and runs it from there. You can even extract the bat from the exe just by using 7zip/rar/winzip or any other archiver.
If you want to convert a bat to an exe for real you should use one of the tools from the web (like this one: http://www.f2ko.de/index.php?lang=en) or concider using a simple script language like AutoIt.
If you pick the second, you can simply execute your bat code with Run("put your bat code in here") and you can compile your script to a "real" exe file.
For an alternative approach, you can basically do the same thing as described in the accepted answer (making a 7z-SFX) with WinRAR. That way, you can also do it with a GUI, and I will try to add some more useful information.
Actually, you can also use the latter approach to generate portable applications and it also works with "converting" every runnable (or openable) file into an .exe.
If you need that "portability hack", you should unpack your .exe or .msi installer with Universal Extractor. Details can be found in this Article, Step 1 to 4. Newer Versions of 7zip or WinRAR also come with comparable features.
Now you add all needed files to the archive. In the easiest case, this is just your .bat script or whatever file you want to "convert" into an .exe applivation. (Step 5 here)
Steps 6 and 7 are just some Settings for the SFX-Archive, 8 is the interesting one, as you select what you actually want to run there. Input the name of your (.bat-)file.
Step 9 lets you select where to unpack to - you do this setting manually and programmatically in the MakeExeFromBat.bat-script.
After this process you created a Portable App in SFX archiever form, enjoy
The word "converting" was put into quotation marks, because running that .exe actually works like this:
The contents of the (SFX-)EXE file are extracted from the "archive part" to a directory as the specified temp directory.
( The config file generated by the script is read. )
The file, that was previously contained in the EXE file and then extracted, is now executed in a new window.
a) This file could besides a .bat be anything - as e.g. an image, a MP3 or a video
b) or also a Python Script (of course your OS needs to know how to deal with that file.
Once finished, the temp files are removed.
You can also derive some limitations from that. If you have a .bat that needs the content of the working directory, you will have a problem. (Say, a batch that renames all files in the current dir from 1 to n.) In some cases that can be dealt with by adding all needed files to the archive too. On Windows Vista and all newer OSes, you might encounter a message box after the script is run. After selecting ‘This program installed correctly’, the message box will not be displayed in the future for this file. Because the EXE file launches in a new window, the typical way of logging output (using the > char) will not work as expected. In order to log the output, you would need to handle this natively in your source script.
All references were already linked, but once again: Big credit goes to Jason Faulkner for providing the Article and 7zip-Approach, binbert for the WinRAR-SFX Solution (which is as hinted much more versatile -> portability) and some credit to creative8 for finding the two and the article comparing them.
Actually, I was develping another solution using AutoHotkey. In my case, I just want to be able to add my .bat to the windows start menu - but the options are not limited to that.
The script itself is just a oneliner and .AHK is easily converted to .exe (I used v1.1.33.09):
run % SubStr(A_ScriptName, 1, -4) ;// run also has the option to run your file minimized or hidden, see the source 2 below
Source 2
What it does is taking its own name, removing the .ahk or .exe respectively (the last 4 characters, hence -4) and running excactly that. Usage could not be easier: you have a runme.bat, so you rename the program I provide to runme.bat.exe. Say you want the .exe to open an image.png - guess what, rename it to image.png.exe. You get the gist - that's it. It dynamically checks its name to find what to run. In my opinion, this is not much less mighty than "unpacking the .bat and then run it", but (again imho) it is much more elegant.
Use it as you wish, I should probably start a public github page or so.
I have a script that changes particular string within files names (the file stores in "my_folder"):
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next
the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.
for example if "my folder" contain:
hello (folder)
hello_.epf (file)
then the script will eventually change the file name to:
hello_v0_1_v0_1._epf (unwanted result)
and i want the result to be:
hello_v0_1._epf
I'll appreciate quick help in this manner.
thanks.
I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.
Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.
for %F in ("g:\my folder\*._epf") do #ren "%F" "%~nF_v0_1%~xF"
If you want to run the command within a batch script, then you need to double all percents.
#echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"
EDIT
The above will append a new version suffix to each file name, before the extension.
If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.
ren "g:\my folder\*_v0_1._epf" "*v0_2.*"
The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.
See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.