Powershell Script to Copy an Unknown number of Files in a Directory

I need to be able to copy an unknown number of files in a folder to an FTP site.  I have the FTP components figured out and working, but am running into a problem when there is only 1 file for upload.  The folder is a queue that users manually
copy files to, so i do not have control of how many files may be pending.  And the file names are always different.
My script performs the following steps.
1. Get a list of files in the directory with the a attribute.  (this prevents subfolders from being processed)
2. Loop thru the list, copying each file.
Here is my problem.
no files. nothing happens. normal and expected
2 or more files. all files upload correctly.  EXPECTED
Filename returned from variable = "True".  File does not exist and upload fails.  ERROR
This is what the script looks like. (There are several Write-Output statements for debugging purposes.
$ftpUploadPath = "$ftpServer/claims/"
$localUploadPath = "$localDir\claims\"
$localSentPath = "$localUploadPath`Sent\"
$listFile = "$localDir\uplist.txt"
Write-Output "Uploading Claim files to $ftpUploadPath ..."
#1 get list of claim files to upload
$list = Get-ChildItem -Name $localUploadPath -Attributes a   
Write-Output $list
Write-Output " "
#2 loop thru list and upload files
if($list.Length -ne 0){
    #Write-Output "  Getting files from list"
    # download each file in the list
    foreach ($file in $list -cne "") {
        Write-Output $file
    } # end foreach file
} #end if($list.Length)
else {
    Write-Output "  No files for upload"
} #end else($list.Length)
Thanks in advance,
Greg

You are writing way too many lines for such a simple task. Try this:
Get-ChildItem -Name $localUploadPath -Attributes a |
ForEach-Object{
Write-Host $_ -fore green
$_
\_(ツ)_/

Similar Messages

  • Powershell Script to copy mobilephone number to telephone number

    Hello Friends,
    I need a powershell script to copy mobilephone attribute to telephone attribute.
    Scenario is below:
    I have Group "Test1" , I need to copy mobilephone attribute to telephone attribute of all the uses in this Group.
    I have created the script but its not giving desired result
    "Get-QADGroup "test1"|
    ForEach-Object{
    Get-QADGroupMember$_.samaccountname}|
    selectsamaccountname,mobilephone
    | export-csvD:\test1.csv
    Import-Csv "D:\test1.csv"|
    %{Get-QADUSer$_.Samaccountname
    | Set-QADUser-ObjectAttributes@{telephonenumber=$_.mobilephone}}"
    My observation:
    1.while I exporting the attribute , i am not getting proper telephone format in CSV which is "+919999XXX1111" 
    2. If Telephonenumber attribute of a user is allready filled , it does not update the attribute. it only update the attribute if it is blank
    Plase help to resolve it.
    this is kind of urgent , thank you all in advance

    Thank you for your reply
    but it is not changing anything however scripts executes perfectly.
    I want to add one more thing here when i export the "mobilePhone" attribute it comes in the format "9.2E+11"
    whereas it should come in the format "+919999XXX111".
    So, as i said earlier if "telephonenumber" fields remains blank it takes the value "9.2E+11" which is error. but it is not replacing if fileld has some value and value format is not correct.

  • How to copy very large number of files from one drive to another???

    I'm a fairly experienced Mac user for serveral years but this problem really has me stumped.
    I'm trying to copy or move 152,000 files from one external drive to another drive. I can highlight (Cmd - A) all the files on the first drive and drag them to the second drive but Finder always shows 32,768 files being copied no matter what I try.
    Any and all suggestions on how to move/copy a large number of files from one external drive to another are greatefully appreciated.
    Thank you in advance,
    Mack

    I would use the command line tool rsync.
    For instance with: rsync -av source-dir destination-dir
    -a The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.
    -v Verbose, so you see the progress.
    Rsync is fast and really, really powerful and many times used in shell scripts and the like to automatically backup and/or sync stuff. Google a bit for more info.

  • Checking Number of Files in a Directory

    Hi
    How can I read the filenames in a directory and determine the number of files in this directory.I need to put all the filenames in a drop down list box on a JSP.
    Please Advise

    FilenameFilter filter = new FilenameFilter() {
      public boolean accept(File dir, String name) {
        File file = new File(dir, name);
        return file.isFile(); // true if file, false if directory
    File dir = new File("c:\\temp"); // the directory we want to list the files from
    File[] files = dir.listFiles(filter);
    // or String[] files = dir.list(filter); I haven't checked the code, so there might be some typos..

  • "Get Info" Reports an Incorrect Number of Files in a Directory

    I have a 17" MacBook Pro with OS X, 10.6.4. I've been relying upon Finder's "Get Info" to determine how many files a directory contains, and I never noticed any problems when using it. However, this afternoon I saw an anomaly with the actual number of files in a directory and what "Get Info" reported.
    I had previously created a new directory and moved 23 files to it. This morning I added three (3) more files to the directory, for a total of 26 files. This afternoon I added another five (5) files for a final total of 31 files in the directory. While Finder's "Get Info" showed that the directory had been updated this afternoon, it would not update the number of files it contains -- it still showed 26 files and the old total file size. I had to log out and log back in again for it to show me the correct file size and number of files.
    Is this normal behavior or was it a glitch in Finder? That is, can I rely upon Finder to tell me the correct number of files in a folder -- or not?
    Is there a way to force Finder to refresh itself? I looked around, but I did not see anything obvious, which is why I tried the logout/login method of refreshing it.
    Thanks,
    Bev in TX

    Welcome to Apple Discussions.
    This is a known issue in Finder. No fix I know of but a decent refresh here.
    -mj

  • Opening unknown number of files

    Does labView have the ability to open a variable number of files?
    If so how would i go about doing this?

    pardon my ignorance... and do WHAT with these files? where does it get the file paths?  I mean after all, lay a file open within a for loop.  wire your number of files to the N terminal.  Then, knowing nothing else... it will prompt the user N number of times to select a file... You will then have opened N file references and have an array of references, if you wire the reference numbers to the edge of your while loop.  Does that accomplish your task?
     I really would like to help, but you have given no information to work with, to allow us to help you. If we knew what your intended functionality is, then we can suggest specific implementations to meet your needs... Otherwise this thread is quickly going to be 30 posts long, frustrating you with no answers and Us, cause we have no idea what you are trying to do.
    Message Edited by Stradis on 08-21-2007 11:39 PM
    Paul <--Always Learning!!!
    sense and simplicity.
    Browse my sample VIs?

  • Finding the number of files in a directory

    hi,
    im trying to return the count of all the files that are available in a specific directory. does anyone know how or what method would spport this?
    Im new to Java and swing please help!
    Thanks

    Hi,
    all you need is provided by the File class. With this class you can work with "physical" files.
    Try this:
    String path = "your directory";
    File file = new File(path);
    String[] fileArray;
    try{
    fileArray = file.list();
    //you need fileArray.length
    System.out.println("Number of files found in "+path+ fileArray.length);
    }catch(SecurityException se){
    //handle exception
    }

  • Number of files in unix directory

    Hi All,
    anyone can help me, how can I get the number of files in specific unix directory and number of files in each
    sub-directory inside that directory?
    Thank You

    Hi,
    Use the following program which will list the files on a unix system.
    Put/modify the logic for getting the file count. (play around)
    REPORT ZUNIXFILES MESSAGE-ID Z1
                      NO STANDARD PAGE HEADING
                      LINE-COUNT 65
                      LINE-SIZE  132.
    Global Variables.                                                   *
    DATA: BEGIN OF TABL OCCURS 0,
              LINE(2000),
          END OF TABL.
    DATA: PARCOM_LOC LIKE RS37A-LINE.
    DATA: SIZE(10) TYPE C,
          LINE     LIKE TABL-LINE,
          SUM(16)  TYPE C.
    DATA: FILE LIKE RS37A-LINE.
    DATA: PATH(255) TYPE C.
    Parameters.                                                       *
    SELECTION-SCREEN SKIP 2.
    PARAMETERS: PARCOM LIKE RS37A-LINE.
    *AT SELECTION-SCREEN.
    AT SELECTION-SCREEN.
      CONDENSE: PARCOM NO-GAPS.
    *TOP-OF-PAGE.
    TOP-OF-PAGE.
      PERFORM WRITE_PAGE_HEADING.
    *INITIALIZATION.
    INITIALIZATION.
      PARCOM = '/usr/sap/*'.               "Only default value
    *START-OF-SELECTION.
    START-OF-SELECTION.
      PERFORM MAKE_LOC.
      PERFORM COLLECT_DATA.
      PERFORM FILE_LIST.
    *AT LINE-SELECTION.
    AT LINE-SELECTION.
      CLEAR FILE.
      FILE = SY-LISEL+55.
      REPLACE '|' WITH SPACE INTO FILE.
      IF SY-LISEL+1(1) = 'd'.
         PATH = FILE.
         PATH+200 = '/*'.
         CONDENSE PATH NO-GAPS.
         SUBMIT ZUNIXFILES
           WITH PARCOM = PATH
           AND RETURN.
      ENDIF.
      SUBMIT ZBROWSEFILE            "Call the browse program
         WITH PARCOM = FILE
         AND RETURN.
    FORM MAKE_LOC.                                                      *
    FORM MAKE_LOC.
      CLEAR: PARCOM_LOC.
      PARCOM_LOC(7)    = 'ls -ld '.
      PARCOM_LOC+7(45) = PARCOM.
    ENDFORM.
    FORM COLLECT_DATA.                                                  *
    FORM COLLECT_DATA.
      REFRESH TABL.
      CALL 'SYSTEM' ID 'COMMAND' FIELD PARCOM_LOC
                    ID 'TAB'     FIELD TABL-SYS.
    ENDFORM.
    FORM FILE_LIST.                                                   *
    FORM FILE_LIST.
      FORMAT RESET.
      CLEAR SUM.
      LOOP AT TABL.
        CLEAR LINE.
        LINE = TABL-LINE.
        WRITE /2 LINE.
        PERFORM VERTICAL_LINES.
        SUM = SUM + SIZE.
      ENDLOOP.
      WRITE:/1(132) SY-ULINE.
    ENDFORM.
    FORM WRITE_PAGE_HEADING.                                          *
    FORM WRITE_PAGE_HEADING.
      WRITE:/1(132) SY-ULINE.
      PERFORM VERTICAL_LINES.
      FORMAT INTENSIFIED.
      WRITE: /2(131) SPACE COLOR 1.
      WRITE:  2    SY-DATUM COLOR 1.
      WRITE: 35 'File Manager' COLOR 1.
      PERFORM VERTICAL_LINES.
      WRITE:/1(132) SY-ULINE.
      PERFORM VERTICAL_LINES.
      FORMAT INTENSIFIED.
      WRITE: /2(131) SPACE  COLOR 3.
      PERFORM VERTICAL_LINES.
      FORMAT RESET.
      WRITE:/1(132) SY-ULINE.
    ENDFORM.
    FORM VERTICAL_LINES.                                              *
    FORM VERTICAL_LINES.
      PERFORM WRITE_VLINE USING ' '  1.
      PERFORM WRITE_VLINE USING ' '  132.
    ENDFORM.
    WRITE_VLINE.                                                      *
    FORM WRITE_VLINE USING MODE POS.
      IF MODE NE 'T'.
        FORMAT INTENSIFIED.
      ENDIF.
      POSITION POS.
      WRITE: SY-VLINE.
      IF MODE NE 'T'.
        FORMAT RESET.
      ENDIF.
    ENDFORM.

  • Powershell script for pinging computers from a text file and displaying output with ip address

    i am currently using the following script to display the computers that are pinging, i also want the script to display the
    ipaddress along with the results but the results are displayed not properly, i want the result to be displayed in a table or list format, what am i missing?
    $ServerName = Get-Content "C:\scripts\computers.txt"
    foreach ($Server in $ServerName) {
    if (test-Connection -ComputerName $Server -Count 2 -Quiet) {
    "$Server is Pinging "
    } else
    {"$Server not pinging"
    foreach ($server in $servername) {
    Get-WmiObject -class "Win32_NetworkAdapterConfiguration"|Select-Object IPAddress|Format-List

    Not sure I completely understand your question but looking at the code you posted I see one fairly simple change that may be what you are missing...your Get-WMIObject statement never passes a server name to the command.  You loop through the list
    you obtained but never actually pass that to anything.  So the simple change would be to just do the following...
    foreach($server in $servername)
    Get-WMIObject -class win32_NetworkAdapterConfiguration -computername $server
    | select IPAddress | FT -auto
    you could also tuck that command into the previous foreach/if/else statement and just do each one that actually responds. 

  • Number of files in a directory

    Hello,
    How can I count how many files are in a directory?

    Now it looks like this:
    - - - - - - - - - - - - - - - - - - - - PHP code - - - - - -
    <?php
    $someValue = 6;
    echo "&someValue=".$someValue."&";
    ?>
    - - - - - - - - - - - - - - - - - - - - AS code - - - - - - -
    var MyLV:LoadVars = new LoadVars();
    MyLV.onLoad = function() {
    trace(MyLV.someValue);
    MyLV.load("
    http://www.sages.hu/galeria/kepgaleria.php");
    It is working, so the Flash traces "6", but how can I do that
    the PHP does not print it out?

  • How to Compare 2 CSV file and store the result to 3rd csv file using PowerShell script?

    I want to do the below task using powershell script only.
    I have 2 csv files and I want to compare those two files and I want to store the comparision result to 3rd csv file. Please look at the follwingsnap:
    This image is csv file only. 
    Could you please any one help me.
    Thanks in advance.
    By
    A Path finder 
    JoSwa
    If a post answers your question, please click &quot;Mark As Answer&quot; on that post and &quot;Mark as Helpful&quot;
    Best Online Journal

    Not certain this is what you're after, but this :
    #import the contents of both csv files
    $dbexcel=import-csv c:\dbexcel.csv
    $liveexcel=import-csv C:\liveexcel.csv
    #prepare the output csv and create the headers
    $outputexcel="c:\outputexcel.csv"
    $outputline="Name,Connection Status,Version,DbExcel,LiveExcel"
    $outputline | out-file $outputexcel
    #Loop through each record based on the number of records (assuming equal number in both files)
    for ($i=0; $i -le $dbexcel.Length-1;$i++)
    # Assign the yes / null values to equal the word equivalent
    if ($dbexcel.isavail[$i] -eq "yes") {$dbavail="Available"} else {$dbavail="Unavailable"}
    if ($liveexcel.isavail[$i] -eq "yes") {$liveavail="Available"} else {$liveavail="Unavailable"}
    #create the live of csv content from the two input csv files
    $outputline=$dbexcel.name[$i] + "," + $liveexcel.'connection status'[$i] + "," + $dbexcel.version[$i] + "," + $dbavail + "," + $liveavail
    #output that line to the csv file
    $outputline | out-file $outputexcel -Append
    should do what you're looking for, or give you enough to edit it to your exact need.
    I've assumed that the dbexcel.csv and liveexcel.csv files live in the root of c:\ for this, that they include the header information, and that the outputexcel.csv file will be saved to the same place (including headers).

  • Using WEBDAV in a Windows PowerShell script

    Hello,
    I am trying to write a Windows PowerShell script to copy a file from my SharePoint site to a DB repository folder using a WEBDAV link.  I am not getting it to work.
    When I copy from SharePoint to SharePoint it works.  I have used my credentials on the script which has the authorizations but it still isn't working.  The response error is a 500 Internal Server error.
    Any ideas?
    Has anyone done this before?
    Thanks,
    Matt

    This is not a solution but more an extra tool to help you find a solution....
    You could download and install the Portal Drive to see what actions/commands are used to communicate via webdav to the KM repository.
    Cheers,
    B

  • PowerShell scripts

    Hello There,
    i need to know about bulk email address hide in office 365 cloud and want know if there is any way through by PowerShell
    script. because there are large number of unwanted users in our Office 365 cloud and want to hide the from Global Address List. any idea how to hide the unwanted email with PowerShell scripts.

    Hi Sajid,
    To connect exchange online via Powershell, please follow this article:
    Connect to Exchange Online using remote PowerShell
    To run "set-Mailbox" against bulk users, please export the mailboxes list and use loop cmdlet:
    foreach( $mailbox in $mailboxs){
    Set-Mailbox -Identity $mailbox -HideFromAddressListsEnabled $true}
    If there is anything else regarding this issue, please feel free to post back.
    Best Regards,                                 
    Anna Wang
    TechNet Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • A powershell script to export then delete exported items every month .

    Hi all ..
    We have an exchange 2013 server running on windows 2012 server .
    I have a mailbox "MB1" hosted on a database with some other mailboxes .
    I want to build a powershell script to export MB1 to a .PST file every month to a shared folder with the name of the month automatically , for example , \\fileserver\share1\November.pst
    after export to PST , delete all messaged in MB1 , to be empty and ready for the next month .
    please, if you have a script for this or KB or guide ... please reply me
    Thanks alot .

    The script you are asking is not available handy. It must be something that has to be written. Start Developing a a script and ping back for any assistance. To help you start of with your script you can include this.
    1. New-MailboxExportRequest -Mailbox MailboxName -FilePath \\MBX-01\PSTFileShare\MailboxName.pst2. Next Step is to check if the ExportRequest was completed. You can use If Condition to check the same.If((Get-MailboxExportRequestStatistics "MailboxName").Status -eq "Completed"){Search-Mailbox MailboxName -DeleteContent -Force}Once you get hold of the logic, you can schedule to run the script once in a month using Task Scheduler.
    M.P.K ~ ( Exchange | 2003/2007/2010/E15(2013)) ~~ Please remember to click “Vote As Helpful" if it really helps and "Mark as Answer” if it answers your question, “Unmark as Answer” if a marked post does not actually answer your question. ~~ This
    Information is provided is "AS IS" and confers NO Rights!!

  • Creating Powershell script to run against Multiple XML Files: Correcting Nordic characters. Renaming only the files that were corrected.

    Greetings,
    I am new to powershell and scripting and would like to create a powershell script to do the following to XML files.
    I would like to change the data received in XML files that contains special Nordic characters: such as 
    ä, Ä, é, ö, Ö, ü, Ü, and ß respectfully.
    The file names that gets drop to me come in with the following naming convention: C54ABC_111120140500_1, C54ABC_111120140500_2, C54ABC_111120140500_3, C54ABC_111120140500_4 all the way to C54ABC_111120140500_12.
    By automating the script, I would like the script to look into a directory containing these files and access the data within it and change the the data that has the special characters to the English Dictionary character equivalent.
    e.g. ä for a, Ä for A, é for e, ö for o, Ö for O, ü for u, Ü for U, and ß for ss.
    Once that step is complete. I would like to rename the file from C54ABC_111120140500_1.xml to C54ABC_111120140500_1_rctf.xml (adding an additional 5 characters to the end of a file that was corrected) if the file did not need any correction then renaming is
    not needed.
    the working directory (Source and destination) will remain in the same location. e.g. C:\Temp\
    Currently I was able to come up with the following, however I am stuck when it comes to the renaming part since, it requires "IF and Else" statments .... I think.
    $Source = "C:\Temp"
    $Destination = "C:\Temp"
    $regex33 = "\*.xml"
    (Get-Content $regex33) |
    ForEach-Object {$_ -replace "ä", "a" -replace "Ä", "A" -replace "é", "e" -replace "ö", "o" -replace "Ö", "O" -replace "ü", "u" -replace "Ü", "U" -replace "ß", "ss"}
    #Rename-Item -NewName {$_.name -replace -whatif}
    Your Assistance and Expertise is greatly appreciated.
    Thank you in advance KC.

    Hi KC
    You could try something like this.
    Let me know how you get on and if you've any questionds.
    cheers, Tim
    $Source = 'C:\Temp'
    Set-Location $source
    $xmlFiles = Get-ChildItem -Path $source -Filter *.xml
    ForEach ($xmlFile in $xmlFiles)
      $content = Get-Content -Path $xmlFile.FullName
      $renameFile = $false
      Switch -Wildcard ($content) {
        '*ä*'
          $renameFile = $true
          $content = $content -creplace 'ä', 'a'
        '*Ä*'
          $renameFile = $true
          $content = $content -creplace 'Ä', 'A'
        '*é*'
          $renameFile = $true
          $content = $content -creplace 'é', 'e'
        '*ö*'
          $renameFile = $true
          $content = $content -creplace 'ö', 'o'
        '*Ö*'
          $renameFile = $true
          $content = $content -creplace 'Ö', 'O'
        '*ü*'
          $renameFile = $true
          $content = $content -creplace 'ü', 'u'
        '*Ü*'
          $renameFile = $true
          $content = $content -creplace 'Ü', 'U'
        '*ß*'
          $renameFile = $true
          $content = $content -creplace 'ß', 'ss'
      If ($renameFile)
        $newFileName = "$($xmlFile.BaseName)_rctf.xml"
        $content | Out-File -FilePath $xmlFile.FullName -Force
        Rename-Item -Path $xmlFile.PSChildName -NewName $newFileName

Maybe you are looking for

  • The worst company i have ever had to deal with.

    on the 9th of may 2013 i tried to order bt infinity 2 with unlimited tv and unlimited phone calls,i tried to order online to be eligible for the £50 sainsburys voucher but when i went to place the order i kept getting an internal server error from th

  • What do I need to do to display a MS Word, Excel or PDF document in browser

    Hi, Right now I have photos loaded and displayed in my HTML document in the browser next to a report... What do I need to do to display a MS Word, Excel or PDF document in a browser? I use the following procedure to load the content to the region of

  • Keep Getting 'Do you want to save changes Msg'

    Hi all Have a small form, two multi-record blocks, everything works ok, have no commits or anything in it but keep getting the message 'do you want to save changes?' if I clear query or exit form, if I press yes I get frm-40400 X amount of records sa

  • VN-Tag with Nexus 1000v and Blades

    Hi folks, A while ago there was a discussion on this forum regarding the use of Catalyst 3020/3120 blades switches in conjunction with VN-tag.  Specifically, you can't do VN-Tag with that Catalyst blade switch sitting inbetween the Nexus 1000V and th

  • Can someone explain to me this code!

    public final static class SessionType public static final SessionType APP_BASE = new SessionType(); public static final SessionType ADVANCED_APP_BASE = new SessionType(); public static final SessionType THREAD_BASE = new SessionType(); public static