Sign in to follow this  
Followers 0
Garp99HasSpoken

[Solved] Can't get DOS rename call to work

3 posts in this topic

#1 ·  Posted (edited)

I've been trying for hours now and can't figure out why this simple thing won't work.

I checked the forums but nothing really works, including this recent thread.

Sometimes I temporarily prefix file names with xx | yy | zz when using mkvmerge to split files.

I then want to be able to simply right-click on 1+ files and click MyRename script.

It will then take that file(s) and strip out the leading characters to rename to the new filename.

Error message in command box is: The system cannot find the file specified.

which seems to be because parameters enclosed in "" and needed because of spaces.

The command prompt is in the correct directory where I right-clicked.

#include <File.au3>
#include <Process.au3>

Dim $file, $newFile, $dir

If ($CmdLine[0] = "0") Then
    MsgBox(0, "No file passed", "File expected to be passed by Context Menu.")
    Exit
EndIf

$file = FileGetLongName($CmdLine[1])

If Not FileExists($file) Then
    MsgBox(4096, "No file found", "No file passed in to script or file no longer exists."  & @CRLF & $file)
    Exit
EndIf

Dim $szDrive, $szDir, $szFName, $szExt, $result
_PathSplit($file, $szDrive, $szDir, $szFName, $szExt)

$file = $szFName

;; check for: xx* | yy* | zz* for temporary MKVMerge files created
If StringRegExp($file, "^[x|y|z]{2}", 0) Then
    $newFile = StringRegExpReplace($file, "^[x|y|z]{2}", "", 1)
    If @error Then
        MsgBox(0, "Pattern Error", "Error - " & @Extended)
    EndIf
Else
    $newFile = ""
EndIf

If StringLen($newFile) Then
    $file = $file & $szExt
    $newFile = $newFile & $szExt

    $result = MsgBox(4+32, "Success", " File: " & $file & @CRLF & "New: " & $newFile _
            & @CRLF & @CRLF & "Path: " & $szDrive & $szDir _
            & @CRLF & @CRLF & "Go ahead and rename this file?" & @CRLF, 15)
    if ($result = 6) Then

;; this is the drive letter + directory taken from split $file -- should also = @WorkingDir
$dir = $szDrive & $szDir

;;;;;; various attempts

;; $result = _RunDOS("ren '" & $dir & $file & "' '" & $dir & $newFile & "'")

;; $result = ShellExecute("cmd /k", "rename", $file, $newFile, @WorkingDir)

        MsgBox(0, "Rename", "ren " & $dir & $file & @CRLF & $dir & $newFile)
;; $result = _RunDOS('ren "' & $dir & $file & '" "' & $dir & $newFile & '"')

;; $result = RunWait(@ComSpec & ' /K REN "' & $dir & '\' & $file & '" "' & $dir & '\' & $newFile & '"', $dir)

;; $result = RunWait(@ComSpec & ' /K REN "' & $file & '" "' & $newFile & '"', $dir)

;;; using /K instead of /C to keep window open for error, /S is supposed to process double-quotes
$result = RunWait(@ComSpec & ' /C REN "' & $file & '" "' & $newFile & '"', $dir, @SW_HIDE)

        If Not $result Then
            MsgBox(0, "Bad Rename", "Could not perform rename: error is " & @error)
        EndIf
    EndIf
Else
    MsgBox(0, "Nothing to do", " File: " & $file & @CRLF & @CRLF & "No changes to make.", 15)
EndIf

Exit
Edited by Garp99HasSpoken

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I've been trying for hours now and can't figure out why this simple thing won't work.

I checked the forums but nothing really works, including this recent thread.

Sometimes I temporarily prefix file names with xx | yy | zz when using mkvmerge to split files.

I then want to be able to simply right-click on 1+ files and click MyRename script.

It will then take that file(s) and strip out the leading characters to rename to the new filename.

Error message in command box is: The system cannot find the file specified.

which seems to be because parameters enclosed in "" and needed because of spaces.

The command prompt is in the correct directory where I right-clicked.

#include <File.au3>
#include <Process.au3>

Dim $file, $newFile, $dir

If ($CmdLine[0] = "0") Then
    MsgBox(0, "No file passed", "File expected to be passed by Context Menu.")
    Exit
EndIf

$file = FileGetLongName($CmdLine[1])

If Not FileExists($file) Then
    MsgBox(4096, "No file found", "No file passed in to script or file no longer exists."  & @CRLF & $file)
    Exit
EndIf

Dim $szDrive, $szDir, $szFName, $szExt, $result
_PathSplit($file, $szDrive, $szDir, $szFName, $szExt)

;; remove dots, if any
$file = StringReplace($szFName, ".", " ")

;; check for: xx* | yy* | zz* for temporary MKVMerge files created
If StringRegExp($file, "^[x|y|z]{2}", 0) Then
    $newFile = StringRegExpReplace($file, "^[x|y|z]{2}", "", 1)
    If @error Then
        MsgBox(0, "Pattern Error", "Error - " & @Extended)
    EndIf
Else
    $newFile = ""
EndIf

If StringLen($newFile) Then
    $result = MsgBox(4+32, "Success", " File: " & $file & @CRLF & "New: " & $newFile _
            & @CRLF & @CRLF & "Path: " & $szDrive & $szDir _
            & @CRLF & @CRLF & "Go ahead and rename this file?" & @CRLF, 15)
    if ($result = 6) Then

;; this is the drive letter + directory taken from split $file -- should also = @WorkingDir
$dir = $szDrive & $szDir

;;;;;; various attempts

;; $result = _RunDOS("ren '" & $dir & $file & "' '" & $dir & $newFile & "'")

;; $result = ShellExecute("cmd /k", "rename", $file, $newFile, @WorkingDir)

        MsgBox(0, "Rename", "ren " & $dir & $file & @CRLF & $dir & $newFile)
;; $result = _RunDOS('ren "' & $dir & $file & '" "' & $dir & $newFile & '"')

;; $result = RunWait(@ComSpec & ' /K REN "' & $dir & '\' & $file & '" "' & $dir & '\' & $newFile & '"', $dir)

;; $result = RunWait(@ComSpec & ' /K REN "' & $file & '" "' & $newFile & '"', $dir)

;;; using /K instead of /C to keep window open for error, /S is supposed to process double-quotes
$result = RunWait(@ComSpec & ' /S /K REN "' & $file & '" "' & $newFile & '"', $dir);; , @SW_HIDE)

        If Not $result Then
            MsgBox(0, "Bad Rename", "Could not perform rename: error is " & @error)
        EndIf
    EndIf
Else
    MsgBox(0, "Nothing to do", " File: " & $file & @CRLF & @CRLF & "No changes to make.", 15)
EndIf

Exit
Wouldn't it be easier to use FileMove?

But your msgbox should give you some of the reason for your commands not working. You have 2 mistakes.

First you have left off the file extension, second you have used the REN command incorrectly I think. The syntax is

REN $FileFullPath $NewFileName

where $NewFileName does not include the path.

You could have found the problem by trying the same thing at the command prompt.

This should work

_RunDos('ren "' & $dir & $file & $szExt & '" "' & $newFile & $szExt & '"')

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Yes, I figured out the missing extension.

Because of that, even FileMove wasn't working :P

$result = RunWait(@ComSpec & ' /C REN "' & $file & '" "' & $newFile & '"', $dir, @SW_HIDE)

because it uses the $dir as a parameter, the rename is performed on local files.

So thanks, I got it working now... updated first post.

Edited by Garp99HasSpoken

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0