FileMove Troubleshooting

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carsonma
Posts: 8
Joined: 21 Oct 2017, 15:27

FileMove Troubleshooting

26 Mar 2024, 16:24

I can't get the files to move with this script. It's not adding new lines to the CSV, either.

I hope that it's not too messy. I have a few diagnostic commands in there that I've been using for troubleshooting.

Code: Select all

#SingleInstance Force
; Set the directory path
dir := "C:\Users\madou\Zotero\storage"

; Check if the directory exists
if !FileExist(dir) {
    MsgBox, 16, Error, Directory not found: %dir%
    ExitApp
}

; Initialize an empty array to store file names and paths
pdfFiles := {}

; Loop through files in the directory
; Last parameter is Mode.
; If mode is blank or omitted, only files are included and subdirectories are not recursed into. Otherwise, specify one or more of the following letters:
    ; D = Include directories (folders).
    ; F = Include files. If both F and D are omitted, files are included but not folders.
    ; R = Recurse into subdirectories (subfolders). If R is omitted, files and folders in subfolders are not included.
Loop, Files, %dir%\*.pdf, RDF
{
    ; Get the file name and full path
    fileName := A_LoopFileName
    filePath := A_LoopFileFullPath

    ; Add file name and path to the array
    pdfFiles[fileName] := filePath
}

; Check if any PDF files were found
if !pdfFiles.Count() {
    MsgBox, 16, Error, No PDF files found in: %dir%
    ExitApp
}

; Generate timestamp
FormatTime, timestamp,, yyyy-MM-dd-HH-mm

; Create the directory if it doesn't exist
moveTo := "C:\Users\madou\OneDrive - UCLA IT Services\Zotero_moved_delete_after_30_days"
outputDirectory := moveTo . "\file_list"
if (!FileExist(outputDirectory))
    FileCreateDir, % outputDirectory

; Create/open the CSV file for writing
outputFile := outputDirectory . "\Zotero_PDF_files.csv"


if (!FileExist(outputFile)) {
    colHeaders := "File Name,File Path,Date_Time`n"
    FileAppend, % colHeaders, % outputFile
}

FileRead, existingCSV, % outputFile

;~ ; Loop through the array and write each key-value pair to the CSV file
for key, value in pdfFiles {
	; Enclose key and value in double quotes
	quotedKey := """" key """"
	quotedValue := """" value """"

	; Create CSV line with quoted key and value
	CSV_line := quotedKey "," quotedValue "," """" timestamp """" "`n"

    if InStr(existingCSV, CSV_line) {
		MsgBox, it ran
        FileAppend, % CSV_line , % outputFile
    }

    FileMove, % quotedValue, % moveTo, 1
}
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: FileMove Troubleshooting

26 Mar 2024, 16:37

Hello,

Here is how I would approach the problem. Start with a new nine-line test script to see if you can move a file from one directory to another. If that works, you can then expand the script.

Code: Select all

#Requires AutoHotkey v1.1.33.11
source      := A_ScriptFullPath
dest        := A_MyDocuments
dirExisted  := FileExist(dest)
fileExisted := FileExist(dest "\" A_ScriptName)
FileMove % source, % dest
MsgBox 64, Result, % "Dir existed   : " dirExisted  "`n"
                   . "File existed  : " fileExisted "`n"
                   . "ErrorLevel    : " ErrorLevel
Learn the values of your variables. You can do this by displaying them, inspecting them, logging them, using ListVars, or using a debugging tool. What is the source value? What is the destination value? Does the directory exist? Does the source file exist? Your current approach answers none of these questions.
carsonma
Posts: 8
Joined: 21 Oct 2017, 15:27

Re: FileMove Troubleshooting

26 Mar 2024, 21:47

Hi. Thanks. I'm trying to figure out how I can use that script without starting from scratch and re-writing everything. That would not seem like the most efficient debugging approach. What is the approach you would use?

I suspect that there is some small syntax error or an absence of quotes, etc. somewhere. (Do paths need quotes? Do I need closing percent signs?) I've only been using MsgBox for debugging so far, and I have not been able to uncover the issue despite placing them everywhere throughout the script. I've gone through every variable with this technique.

I tried ListVars. I can't see any issues there. Is there a way to run it while debugging so I can see the values as the loop runs?

The files exist when I run it. I think that is the least likely problem since I am literally creating the array with Loop, Files.
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: FileMove Troubleshooting

26 Mar 2024, 22:13

A simple way to start is below, in three steps.
  1. Change lines 2 and 3 of the script below.
  2. Run the short revision.
  3. See if you succeeded in moving the file.
If the move fails, you now have a total of ten lines to debug. This will be easier and faster than what you are currently doing.

If the move succeeds, you now know how to move a file. You can then add one more incremental new part to your script, and re-test it.

Code: Select all

#Requires AutoHotkey v1.1.33.11
source := A_ScriptFullPath ; Full path to the source file
dest   := A_MyDocuments    ; Target directory
SplitPath source, fn
dirExisted  := FileExist(dest)
fileExisted := FileExist(dest "\" fn)
FileMove % source, % dest
MsgBox 64, Result, % "Dir existed   : " dirExisted  "`n"
                   . "File existed  : " fileExisted "`n"
                   . "ErrorLevel    : " ErrorLevel
The AHK documentation describes debugging tools. Many coders find them useful.

If your script has a syntax error, AHK will alert you by showing you an error message.

The test script here demonstrates how you can understand the values of some variables. You could add to it by displaying the source and destination values. Directly showing whether your target directory and target file exist is useful for troubleshooting.

Why might a file move fail?
  1. Source file is not found.
  2. Destination file already exists.
  3. Destination directory does not exist.
  4. One of the paths cannot be accessed.
  5. One of the files is in use and cannot be accessed.
  6. Wrong source file was specified.
  7. Wrong destination was specified.
Checking for each of these possibilities is straightforward, but you cannot guess the answers. Use the script to inform you of the answers.

If you are failing to move a file, then adding loops, string modifications, and other functionality does not serve any real purpose, but it does serve to slow and impede your resolution of moving the file.

If I had your original script and did not want to alter much about it, I would add two lines immediately before the file move, and one line after it. The first line would display the source. The second line would display the destination. The third line would display the ErrorLevel following the move.

You can learn more about variables by reading the documentation, or trying scripts.

Code: Select all

#Requires AutoHotkey v1.1.33.11
x := 5
MsgBox x + 1     ; Literal text for the command's parameter
MsgBox %x%       ; Legacy syntax for variables can also be used
MsgBox % x + 1   ; Forced expression (computation of a number)
MsgBox % "x + 1" ; Forced expression (a text string)
In general, AHK v1 uses parameters for its commands. These parameters generally use literal text, but parameters that are always numeric will (I think with few exceptions) accept any expression (that evaluates to a number). Other parameters may use expressions instead of literals if the expression is forced via a leading %. When the leading % is used to force an expression for a command parameter, there is no closing character or indicator. From the example, you can see that to make a string into an expression, the string is quoted.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 282 guests