Let's say I have a bunch of filenames with the names of fruits in them. I want to auto-rename them based upon a folder full of reference files (dummy txt files which contain the names of fruits, a period, then the name of a dessert).
apple.tart, grape.jelly, kiwi.cake, mango.icecream, banana.pudding, cherry.cobbler, etc
I want to select all the files to be renamed, and drag them onto my script.
If a file in the loop already contains a certain combo, such as "cherry.cobbler", I simply want the dummyfile to be discarded, and the file should NOT be renamed "cherry.cobbler.cobbler"
If a file in the loop contains the word "kiwi", I want it to be changed so that it contains "kiwi.cake".
If a file in the loop contains a fruit not listed, I want a catchall string to be added. So "kumquat" would become "kumquat.nodessert"
It is condition #3 which is causing me trouble. I can't seem to come up with the right syntax to specify when the last dummyfile has been checked.
here's some pseudo code
Loop %0%
{
Path := %A_Index%
Loop %Path%, 1
LongPath = %A_LoopFileLongPath%
SplitPath LongPath, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
Loop thru folder of fruit combos
{
Stringsplit filenames from fruit-combos folder into %fruit% and %dessert%
If OutNameNoExt contains %fruit%.%dessert%
{
FileDelete fruit.combo dummyfile
continue; skip to next file to rename
)
If OutNameNoExt contains %fruit%
{
FileDelete fruit.combo dummyfile
StringReplace %fruit% with %fruit%.%dessert%
continue; skip to next file to rename
)
If OutNameNoExt not contains fruit.combo AND all dummy files have been checked
{
StringReplace %fruit% with %fruit%.nodessert
)
}
; proceed with next selected file
}
put condition 3 outside the inner loop and it seems to work
Related
I need to save files to a directory,if the file exists I need to rename the file.So I'm using while loop for the same.The strange thing is , the while loop understands that there exists a file in the first execution and increments the counter and assign the variable, but when the newly generated file name exists it seems not to detect that
let mappedFilename=inputfile.lastPathComponent
var outputfile=outdir+"/"+mappedFilename
var filenamecounter=1
while(FileManager.default.fileExists(atPath: outputfile))
{
var test=URL(fileURLWithPath: outputfile).deletingPathExtension().absoluteString + "_" +
filenamecounter.description+".pdf";
//doing this because, if directly assigning to output file,the assignment seems not to work
outputfile=test
filenamecounter=filenamecounter+1
}
Example
You can see the return value of the loop here
I am trying to batch rename many files within the same folder on my operating system.
I have a dataset with two variables: oldname and newname.
There are more file names in the dataset then actual files that need to be renamed so I would like to build something that matches the file with the observation in the dataset, renames the matched file, and then moves it to the new location.
For example, I have a three files in a location:
filepath/to/my/files/file1.jpg
filepath/to/my/files/file2.jpg
filepath/to/my/files/file3.jpg
A dataset with string variables:
Dataset
newname oldname
a pink files/file1.jpg
b blue files/file4.jpg
c purple files/file6.jpg
d green files/file3.jpg
e red files/file2.jpg
Here is the desired output of the program:
filepath/for/matches/pink.jpg
filepath/for/matches/red.jpg
filepath/for/matches/green.jpg
Do I need to use the ! operator to achieve what i want?
EDIT:
So far, I've got a method of moving the matches but not for renaming them:
global dir "/filepath"
global old "$dir/to/my/"
global new "$dir/for/matches"
ssc install mvfiles
preserve
keep if oldname!=.
foreach i in oldname{
mvfiles, infolder($old) outfolder($new) match(substr(`i',6,9))
}
restore
Not necessarily. You can achieve what you want by simply using the copy command.
The following should work:
clear
input str1 letter str10 newname str15 oldname
"a" "pink" "files/file1"
"b" "blue" "files/file4"
"c" "purple" "files/file6"
"d" "green" "files/file3"
"e" "red" "files/file2"
end
local inpath "filepath/to/my/files/"
local outpath "different/filepath/for/matches/"
local files : dir "`inpath'" files "*.jpg"
local obs = _N
foreach fil of local files {
forvalues i = 1 / `obs' {
local oldname = oldname[`i']
if substr("`fil'", 1, strpos("`fil'", ".") - 1) == substr("`oldname'", 7, .) {
local newname = newname[`i']
copy "`inpath'`fil'" " `outpath'`newname'.jpg"
}
}
}
Just replace the local macros inpath and outpath with your desired paths.
Note that If you also want to delete the file after you copy it, then just add the following after the copy command:
erase "`inpath'`fil'"
Trying to read a CSV file in Auto Hot Key and line by line split the line by "," to pull out the last two columns of each line. Currently just trying to get the string to split into an array. I can print each line with the line
MsgBox, A_LoopReadLine but cannot split the string inside of the variable.
Have tried StringSplit and StrSplit but I am sure the syntax is incorrect.
MyArray := Object()
Loop, read, %fileop%
{
MyArray.Insert(A_LoopReadLine) ; Append this line to the array.
index := 1
MsgBox, %A_LoopReadLine%
;MyArray.
;MsgBox, % StrSplit(A_LoopReadLine ,",")
}
Loop % MyArray.Length()
MsgBox % StrSplit(MyArray[A_Index],",")
Trying to read a CSV file in Auto Hot Key and line by line split the
line by "," to pull out the last two columns of each line.
MyArray := Object()
Loop, Read, %fileop%
MyArray[A_Index]:=StrSplit(A_LoopReadLine,",")
This will store your csv file in MyArray[row][column] format. E.g to access second item in fifth row: MyArray[5][2]
for k,v in MyArray
v.RemoveAt(1,v.Length()-2)
Above will remove all but last two items from each row.
Documentation:
https://autohotkey.com/docs/commands/For.htm
https://autohotkey.com/docs/objects/Object.htm#RemoveAt_v1121+
https://autohotkey.com/docs/objects/Object.htm#Length
Edit:
And to as to why your code did not work. It kinda did.
The thing is that StrSplit() returns object, array so with below line you were trying to display object in MsgBox, this is not allowed.
MsgBox % StrSplit(MyArray[A_Index],",")
This for example would work:
MsgBox % StrSplit(MyArray[A_Index],",")[1]
I need to edit standalone.bat file using ahk script. I want to increase my heap size using ahk so below is line where i have to change heap in my bat file. Now i have trying to edit this using StringReplace and FileAppend but FileAppend keeps on appending string to the end
from
set "JAVA_OPTS=-Dprogram.name=%PROGNAME% -Xms64M -Xmx1426M %JAVA_OPTS%"
to
set "JAVA_OPTS=-Dprogram.name=%PROGNAME% -Xms64M -Xmx1426M %JAVA_OPTS%"xms000M
I am new to .ahk, i have tried this using some search
Loop, read, C:\standalone.bat
{
Line = %A_LoopReadLine%
replaceto = xms000M
IfInString, Line, Xmx1426M
, Line, replaceto, %Line%, %replaceto%
FileAppend, %replaceto%`n
StringReplace FileAppend
}
Is it possible to replace middle string using ahk. thanks
Fileappend will always append to the end of a file. Why do you want to prevent a temporary deletion of your batch file?
Typically, in ahk, you'd do it like this..
batFile = C:\standalone.bat
output := ""
Loop, read, %batFile%
{
Line = %A_LoopReadLine%
IfInString, Line, Xmx1426M
{
StringReplace, Line, Line, Xmx1426M, xms000M
; note: Regular Expressions can be used like Line := regExReplace(Line, "...", "...")
}
output .= Line . "`n" ; note: this is the same as if to say output = %output%%Line%`n or output := output . line "`n"
}
FileDelete, %batFile%
FileAppend, %output%, %batFile%
This will delete your file for some little milliseconds, just to recreate it with the new content afterwards. I don't really see any difference to editing it without deletion, because in either way, you'll need write access to the file.
Some words about your code sample:
IfInString, Line, Xmx1426M
, Line, replaceto, %Line%, %replaceto%
will be interpreted as
"If the string 'Line' contains 'Xmx1426M , Line, replaceto, %Line%, %replaceto%'"
which does not make any greater sense.
FileAppend, %replaceto%\n is lacking a destination file.
StringReplace FileAppend: these are two commands without any further parameters. You must never put two non-function-commands in the same line!
I am learning to use gulp.I took a eg scenario in which I tried to copy the files based on the name if it is odd move it to odd folder otherwise move it to even. But some where destination folder is messed up. here is the folder structure and code.
1
--my
----new
-----2.txt
----1.txt
----2.txt
g = gulp.src '**/*.txt', cwd: '1'
g.pipe map (file,cb)->
filename = path.basename file.path, path.extname(file.path)
if filename % 2
dest = 'odd'
else
dest = 'even'
debugger
console.log 'destination ',dest
g.pipe gulp.dest dest
cb null,file
It is copying the even file names to odd folder.It is about the destination folder being remembered(closure I think)
If you literally only have two output destinations, the simplest solution might be to use the gulp-if plugin, like so (I don't use CoffeeScript, so you'll have to convert it yourself):
var _if = require('gulp-if');
function fileIsOdd(file) {
return path.basename(file.path, path.extname(file.path)) % 2;
}
// in your task
gulp.src('**/*.txt', {cwd: '1'})
.pipe(_if(fileIsOdd, gulp.dest('odd'), gulp.dest('even')))
What this does is process the individual file with the fileIsOdd function. If the function returns a truthy value, then the first argument is processed, otherwise the second argument is processed.